public void DefineConstructor(MethodAttributes methodAttributes, Type[] parameterTypes, CallingConventions callingConvention)
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);

            FieldBuilder fieldBuilderA = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
            FieldBuilder fieldBuilderB = type.DefineField("TestField", typeof(int), FieldAttributes.Private);

            ConstructorBuilder constructor     = type.DefineConstructor(methodAttributes, callingConvention, parameterTypes);
            ILGenerator        ctorIlGenerator = constructor.GetILGenerator();

            if (parameterTypes.Length != 0)
            {
                // Calling base class constructor
                ctorIlGenerator.Emit(OpCodes.Ldarg_0);
                ctorIlGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0]));

                ctorIlGenerator.Emit(OpCodes.Ldarg_0);
                ctorIlGenerator.Emit(OpCodes.Ldarg_1);
                ctorIlGenerator.Emit(OpCodes.Stfld, fieldBuilderA);

                ctorIlGenerator.Emit(OpCodes.Ldarg_0);
                ctorIlGenerator.Emit(OpCodes.Ldarg_2);
                ctorIlGenerator.Emit(OpCodes.Stfld, fieldBuilderB);
            }
            ctorIlGenerator.Emit(OpCodes.Ret);
            Helpers.VerifyConstructor(constructor, type, methodAttributes, parameterTypes);
        }
示例#2
0
        public void DefineDefaultConstructor(MethodAttributes attributes, MethodAttributes expectedAttributes)
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineDefaultConstructor(attributes);

            Helpers.VerifyConstructor(constructor, type, attributes, CallingConventions.Standard, new Type[0]);
        }
        public void DefineConstructor_NullRequiredAndOptionalCustomModifiers(MethodAttributes attributes, Type[] parameterTypes, CallingConventions callingConvention)
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(attributes, callingConvention, parameterTypes);

            constructor.GetILGenerator().Emit(OpCodes.Ret);

            Helpers.VerifyConstructor(constructor, type, attributes, callingConvention, parameterTypes);
        }
示例#4
0
        public void DefineTypeInitializer()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);

            FieldBuilder       greetingField = type.DefineField("Greeting", typeof(string), FieldAttributes.Private | FieldAttributes.Static);
            ConstructorBuilder constructor   = type.DefineTypeInitializer();

            // Generate IL for the method. The constructor calls its base class
            // constructor. The constructor stores its argument in the private field.
            ILGenerator constructorIlGenerator = constructor.GetILGenerator();

            constructorIlGenerator.Emit(OpCodes.Ldstr, "hello");
            constructorIlGenerator.Emit(OpCodes.Stsfld, greetingField);
            constructorIlGenerator.Emit(OpCodes.Ret);

            Helpers.VerifyConstructor(constructor, type, MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.SpecialName, CallingConventions.Standard, new Type[0]);

            Type      createdType  = type.CreateTypeInfo().AsType();
            FieldInfo createdField = createdType.GetField("Greeting", BindingFlags.NonPublic | BindingFlags.Static);

            Assert.Equal("hello", createdField.GetValue(Activator.CreateInstance(createdType)));
        }