public void Activator_CreateInstance_Generic()
        {
            Choice1 c = Activator.CreateInstance <Choice1>();

            Assert.AreEqual(1, c.I);

            Activator.CreateInstance <DateTime>();
            Activator.CreateInstance <StructTypeWithoutReflectionMetadata>();
        }
        public void Activator_TestActivatorOnNonActivatableFinalizableTypes()
        {
            // On runtimes where the generic Activator is implemented with special codegen intrinsics, we might allocate
            // an uninitialized instance of the object before we realize there's no default constructor to run.
            // Make sure this has no observable side effects.
            AssertExtensions.ThrowsAny <MissingMemberException>(() => { Activator.CreateInstance <TypeWithPrivateDefaultCtorAndFinalizer>(); });

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Assert.IsFalse(TypeWithPrivateDefaultCtorAndFinalizer.WasCreated);
        }
        public void Activator_CreateInstance_Generic_Invalid()
        {
#if !WindowsCE
            AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <int[]>()); // Cannot create array type
#else
            Activator.CreateInstance <int[]>();                                                            // Can create array type
#endif

            AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <TypeWithoutDefaultCtor>());        // Type has no default constructor
            AssertExtensions.Throws <TargetInvocationException>(() => Activator.CreateInstance <TypeWithDefaultCtorThatThrows>()); // Type has a default constructor that throws
#if !WindowsCE
            AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <AbstractTypeWithDefaultCtor>());   // Type is abstract
            AssertExtensions.ThrowsAny <MissingMemberException>(() => Activator.CreateInstance <IInterfaceType>());                // Type is an interface
#else
            AssertExtensions.ThrowsAny <MemberAccessException>(() => Activator.CreateInstance <AbstractTypeWithDefaultCtor>());    // Type is abstract
            AssertExtensions.ThrowsAny <MemberAccessException>(() => Activator.CreateInstance <IInterfaceType>());                 // Type is an interface
#endif
        }
        public void Activator_CreateInstance()
        {
            // Passing null args is equivalent to an empty array of args.
            Choice1 c = (Choice1)(Activator.CreateInstance(typeof(Choice1), null));

            Assert.AreEqual(1, c.I);

            c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { }));
            Assert.AreEqual(1, c.I);

            c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { 42 }));
            Assert.AreEqual(2, c.I);

            c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { "Hello" }));
            Assert.AreEqual(3, c.I);

            c = (Choice1)(Activator.CreateInstance(typeof(Choice1), new object[] { 5.1, "Hello" }));
            Assert.AreEqual(4, c.I);

            Activator.CreateInstance(typeof(StructTypeWithoutReflectionMetadata));
        }