public static InstantiateType ( |
||
assembly | The assembly. | |
typeName | string | Name of the type. |
리턴 | object |
public void InstantiateTypeWithCtorArgs() { Type type = typeof(TestObject); ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(string), typeof(int) }); object foo = ObjectUtils.InstantiateType(ctor, new object[] { "Yakov Petrovich Golyadkin", 39 }); Assert.IsNotNull(foo, "Failed to instantiate an instance of a valid Type."); Assert.IsTrue(foo is TestObject, "The instantiated instance was not an instance of the Type that was passed in."); TestObject obj = foo as TestObject; Assert.AreEqual("Yakov Petrovich Golyadkin", obj.Name); Assert.AreEqual(39, obj.Age); }
public void InstantiateTypeThrowingWithinPublicConstructor() { try { ObjectUtils.InstantiateType(typeof(ThrowingWithinConstructor)); Assert.Fail(); } catch (FatalReflectionException ex) { // no nasty "TargetInvocationException" is in between! Assert.AreEqual(typeof(ThrowingWithinConstructorException), ex.InnerException.GetType()); } }
public void InstantiateTypeWithBadCtorArgs() { Type type = typeof(TestObject); ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(string), typeof(int) }); try { ObjectUtils.InstantiateType(ctor, new object[] { 39, "Yakov Petrovich Golyadkin" }); Assert.Fail("Should throw an error"); } catch { // ok... } }
public void InstantiateTypeWithNullCtor() { ObjectUtils.InstantiateType(typeof(IList).GetConstructor(Type.EmptyTypes), new object[] { }); }
public void InstantiateTypeWithTypeExposingNoZeroArgCtor() { ObjectUtils.InstantiateType(typeof(NoZeroArgConstructorType)); }
public void InstantiateTypeWithInterfaceType() { ObjectUtils.InstantiateType(typeof(IList)); }
public void InstantiateTypeWithAbstractType() { ObjectUtils.InstantiateType(typeof(AbstractType)); }
public void InstantiateTypeWithOpenGenericType() { ObjectUtils.InstantiateType(typeof(Dictionary <,>)); }
public void InstantiateTypeWithNullType() { ObjectUtils.InstantiateType(null); }
public void InstantiateTypeWithNullCtor() { Assert.Throws <ArgumentNullException>(() => ObjectUtils.InstantiateType(typeof(IList).GetConstructor(Type.EmptyTypes), new object[] { })); }
public void InstantiateTypeWithTypeExposingNoZeroArgCtor() { Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(NoZeroArgConstructorType))); }
public void InstantiateTypeWithInterfaceType() { Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(IList))); }
public void InstantiateTypeWithAbstractType() { Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(AbstractType))); }
public void InstantiateTypeWithOpenGenericType() { Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(Dictionary <,>))); }
public void InstantiateTypeWithNullType() { Assert.Throws <ArgumentNullException>(() => ObjectUtils.InstantiateType(null)); }