InstantiateType() public static method

Instantiates the type using the assembly specified to load the type.
This is a convenience in the case of needing to instantiate a type but not wanting to specify in the string the version, culture and public key token.
/// If the or is /// /// If cannot load the type from the assembly or the call to InstantiateType(Type) fails. ///
public static InstantiateType ( Assembly assembly, string typeName ) : object
assembly System.Reflection.Assembly The assembly.
typeName string Name of the type.
return object
Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
 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());
     }
 }
Ejemplo n.º 3
0
        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...
            }
        }
Ejemplo n.º 4
0
 public void InstantiateTypeWithNullCtor()
 {
     ObjectUtils.InstantiateType(typeof(IList).GetConstructor(Type.EmptyTypes), new object[] { });
 }
Ejemplo n.º 5
0
 public void InstantiateTypeWithTypeExposingNoZeroArgCtor()
 {
     ObjectUtils.InstantiateType(typeof(NoZeroArgConstructorType));
 }
Ejemplo n.º 6
0
 public void InstantiateTypeWithInterfaceType()
 {
     ObjectUtils.InstantiateType(typeof(IList));
 }
Ejemplo n.º 7
0
 public void InstantiateTypeWithAbstractType()
 {
     ObjectUtils.InstantiateType(typeof(AbstractType));
 }
Ejemplo n.º 8
0
 public void InstantiateTypeWithOpenGenericType()
 {
     ObjectUtils.InstantiateType(typeof(Dictionary <,>));
 }
Ejemplo n.º 9
0
 public void InstantiateTypeWithNullType()
 {
     ObjectUtils.InstantiateType(null);
 }
Ejemplo n.º 10
0
 public void InstantiateTypeWithNullCtor()
 {
     Assert.Throws <ArgumentNullException>(() => ObjectUtils.InstantiateType(typeof(IList).GetConstructor(Type.EmptyTypes), new object[] { }));
 }
Ejemplo n.º 11
0
 public void InstantiateTypeWithTypeExposingNoZeroArgCtor()
 {
     Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(NoZeroArgConstructorType)));
 }
Ejemplo n.º 12
0
 public void InstantiateTypeWithInterfaceType()
 {
     Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(IList)));
 }
Ejemplo n.º 13
0
 public void InstantiateTypeWithAbstractType()
 {
     Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(AbstractType)));
 }
Ejemplo n.º 14
0
 public void InstantiateTypeWithOpenGenericType()
 {
     Assert.Throws <FatalReflectionException>(() => ObjectUtils.InstantiateType(typeof(Dictionary <,>)));
 }
Ejemplo n.º 15
0
 public void InstantiateTypeWithNullType()
 {
     Assert.Throws <ArgumentNullException>(() => ObjectUtils.InstantiateType(null));
 }