/// <summary> /// Returns an instance of the <paramref name="type" /> on which the method is invoked. /// </summary> /// <typeparam name="TArg1">The type of the first argument to pass to the constructor.</typeparam> /// <typeparam name="TArg2">The type of the second argument to pass to the constructor.</typeparam> /// <typeparam name="TArg3">The type of the third argument to pass to the constructor.</typeparam> /// <param name="type">The type on which the method was invoked.</param> /// <param name="argument1">The first argument to pass to the constructor.</param> /// <param name="argument2">The second argument to pass to the constructor.</param> /// <param name="argument3">The third argument to pass to the constructor.</param> /// <returns>An instance of the given <paramref name="type" />.</returns> public static object GetInstance <TArg1, TArg2, TArg3>( this Type type, TArg1 argument1, TArg2 argument2, TArg3 argument3) { return(InstanceCreationFactory <TArg1, TArg2, TArg3> .CreateInstanceOf(type, argument1, argument2, argument3)); }
public void InstanceCreationFactoryCreatesTypeWithNoParameters() { object o = InstanceCreationFactory.GetInstance(typeof(TestClass)); TestClass result = o as TestClass; Assert.IsNotNull(result); }
public void InstanceCreationFactoryCreatesTypeWithOneParameter() { object o = InstanceCreationFactory.GetInstance(typeof(TestClass), 1); TestClass result = o as TestClass; Assert.IsNotNull(result); Assert.AreEqual(1, result.A); }
public void InstanceCreationFactoryCreatesTypeWithMultipleConstructorMethods() { InstanceCreationFactory.GetInstance(typeof(TestClass)); InstanceCreationFactory.GetInstance(typeof(TestClass), 1); InstanceCreationFactory.GetInstance(typeof(TestClass), 1, 2); InstanceCreationFactory.GetInstance(typeof(TestClass), 1, 2, 3); var result1 = InstanceCreationFactory.GetInstance(typeof(TestClass)) as TestClass; var result2 = InstanceCreationFactory.GetInstance(typeof(TestClass), 1) as TestClass; var result3 = InstanceCreationFactory.GetInstance(typeof(TestClass), 1, 2) as TestClass; var result4 = InstanceCreationFactory.GetInstance(typeof(TestClass), 1, 2, 3) as TestClass; Assert.IsNotNull(result1); Assert.IsNotNull(result2); Assert.IsNotNull(result3); Assert.IsNotNull(result4); }
/// <summary> /// 获取3个构造参数的实例 /// </summary> /// <typeparam name="TArg1">参数类型</typeparam> /// <typeparam name="TArg2">参数类型</typeparam> /// <typeparam name="TArg3">参数类型</typeparam> /// <typeparam name="T"></typeparam> /// <param name="type">实例类型</param> /// <param name="argument1">参数值</param> /// <param name="argument2">参数值</param> /// <param name="argument3">参数值</param> /// <returns></returns> public static T GetInstance <TArg1, TArg2, TArg3, T>(string type, TArg1 argument1, TArg2 argument2, TArg3 argument3) where T : class, new() { return(InstanceCreationFactory <TArg1, TArg2, TArg3, T> .CreateInstanceOf(Type.GetType(type), argument1, argument2, argument3)); }
/// <summary> /// 获取3个构造参数的实例 /// </summary> /// <typeparam name="TArg1">参数类型</typeparam> /// <typeparam name="TArg2">参数类型</typeparam> /// <typeparam name="TArg3">参数类型</typeparam> /// <param name="type">实例类型</param> /// <param name="argument1">参数值</param> /// <param name="argument2">参数值</param> /// <param name="argument3">参数值</param> /// <returns></returns> public static object GetInstance <TArg1, TArg2, TArg3>(string type, TArg1 argument1, TArg2 argument2, TArg3 argument3) { return(InstanceCreationFactory <TArg1, TArg2, TArg3> .CreateInstanceOf(Type.GetType(type), argument1, argument2, argument3)); }
/// <summary> /// Returns an instance of the <paramref name="type"/> on which the method is invoked. /// </summary> /// <typeparam name="TArg">The type of the argument to pass to the constructor.</typeparam> /// <param name="type">The type on which the method was invoked.</param> /// <param name="argument">The argument to pass to the constructor.</param> /// <returns>An instance of the given <paramref name="type"/>.</returns> public static object GetInstance <TArg>(this Type type, TArg argument) { return(InstanceCreationFactory <TArg> .CreateInstanceOf(type, argument)); }
public static object GetInstance <TArg1, TArg2, TArg3>( this Type type, TArg1 argument1, TArg2 argument2, TArg3 argument3) { Contract.Requires <ArgumentNullException>(type != null); return(InstanceCreationFactory <TArg1, TArg2, TArg3> .CreateInstanceOf(type, argument1, argument2, argument3)); }