示例#1
0
        public static void CreateInstance()
        {
            // Passing null args is equivalent to an empty array of args.
            Choice1 c = (Choice1)(Activator.CreateInstance(typeof(Choice1), null));

            Assert.Equal(1, c.I);

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

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

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

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

            Activator.CreateInstance(typeof(StructTypeWithoutReflectionMetadata));

            StructWithPublicDefaultConstructor s = (StructWithPublicDefaultConstructor)Activator.CreateInstance(typeof(StructWithPublicDefaultConstructor));

            Assert.True(s.ConstructorInvoked);
        }
示例#2
0
#pragma warning disable SA1129
        public ClassWithOptionalArgsCtorWithStructs(
            DateTime dateTime                    = new DateTime(),
            DateTime dateTimeDefault             = default(DateTime),
            TimeSpan timeSpan                    = new TimeSpan(),
            TimeSpan timeSpanDefault             = default(TimeSpan),
            DateTimeOffset dateTimeOffset        = new DateTimeOffset(),
            DateTimeOffset dateTimeOffsetDefault = default(DateTimeOffset),
            Guid guid                        = new Guid(),
            Guid guidDefault                 = default(Guid),
            CustomStruct customStruct        = new CustomStruct(),
            CustomStruct customStructDefault = default(CustomStruct),
            ConsoleColor?color               = ConsoleColor.DarkGreen,
            ConsoleColor?colorNull           = null,
            int?integer                      = 12,
            int?integerNull                  = null,
            StructWithPublicDefaultConstructor structWithConstructor = default
            )
#pragma warning restore SA1129
        {
            Color                 = color;
            ColorNull             = colorNull;
            Integer               = integer;
            IntegerNull           = integerNull;
            StructWithConstructor = structWithConstructor;
        }