Example #1
0
        public void SetCustomAttribute2_CustomBuilder_Null()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, CallingConventions.Standard,
                new Type [1] {
                typeof(int)
            });

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

            try {
                cb.SetCustomAttribute((CustomAttributeBuilder)null);
                Assert.Fail("#A1");
            } catch (ArgumentNullException ex) {
                Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#A2");
                Assert.IsNull(ex.InnerException, "#A3");
                Assert.IsNotNull(ex.Message, "#A4");
                Assert.AreEqual("customBuilder", ex.ParamName, "#A5");
            }

            genClass.CreateType();

            try {
                cb.SetCustomAttribute((CustomAttributeBuilder)null);
                Assert.Fail("#B1");
            } catch (ArgumentNullException ex) {
                Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#B2");
                Assert.IsNull(ex.InnerException, "#B3");
                Assert.IsNotNull(ex.Message, "#B4");
                Assert.AreEqual("customBuilder", ex.ParamName, "#B5");
            }
        }
Example #2
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public override void SetAttributes(HappilAttributes attributes)
        {
            if (attributes != null)
            {
                foreach (var attribute in attributes.GetAttributes())
                {
                    m_ConstructorBuilder.SetCustomAttribute(attribute);
                }
            }
        }
        /// <summary>
        /// Adds a <see cref="DebuggerHiddenAttribute"/> to a constructor.
        /// </summary>
        /// <param name="constructor">The constructor to add the attribute.</param>
        private static void AddDebuggerHiddenAttribute(ConstructorBuilder constructor)
        {
            Type attributeType = typeof(DebuggerHiddenAttribute);
            CustomAttributeBuilder attribute = new CustomAttributeBuilder(attributeType.GetConstructor(new Type[0]), new object[0]);

            constructor.SetCustomAttribute(attribute);
        }
        public void SetCustomAttribute_ConstructorBuilder_ByteArray_NullConstructorBuilder_ThrowsArgumentNullException()
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });

            Assert.Throws <ArgumentNullException>("con", () => constructor.SetCustomAttribute(null, new byte[] { 01, 00, 05, 00, 00, 00 }));
        }
        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
            ILGenerator        ilGenerator = constructor.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ldarg_1);

            ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[1] {
                typeof(int)
            });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[1] {
                2
            });

            constructor.SetCustomAttribute(attributeBuilder);
            Type createdType = type.CreateTypeInfo().AsType();

            ConstructorInfo createdConstructor = createdType.GetConstructor(new Type[0]);

            Attribute[] customAttributes = createdConstructor.GetCustomAttributes(true).ToArray();

            Assert.Equal(1, customAttributes.Length);
            Assert.Equal(2, ((IntAllAttribute)customAttributes[0])._i);
        }
        private void AddDebuggerHiddenAttribute(ConstructorBuilder constructor)
        {
            var type          = typeof(DebuggerHiddenAttribute);
            var customBuilder = new CustomAttributeBuilder(type.GetConstructor(new Type[0]), new object[0]);

            constructor.SetCustomAttribute(customBuilder);
        }
Example #7
0
        public void TestSetCustomAttribute()
        {
            AssemblyName       myAssemblyName  = new AssemblyName("EmittedAssembly");
            AssemblyBuilder    myAssembly      = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
            ModuleBuilder      myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssembly, "Module1");
            TypeBuilder        myTypeBuilder   = myModuleBuilder.DefineType("HelloWorld", TypeAttributes.Public);
            ConstructorBuilder myConstructor   = myTypeBuilder.DefineConstructor(
                MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
            ILGenerator myILGenerator = myConstructor.GetILGenerator();

            myILGenerator.Emit(OpCodes.Ldarg_1);

            ConstructorInfo myConstructorInfo = typeof(CBMyAttribute2).GetConstructor(new Type[1] {
                typeof(int)
            });
            CustomAttributeBuilder attributeBuilder =
                new CustomAttributeBuilder(myConstructorInfo, new object[1] {
                2
            });

            myConstructor.SetCustomAttribute(attributeBuilder);
            Type myHelloworld = myTypeBuilder.CreateTypeInfo().AsType();

            ConstructorInfo myReflectConstructorInfo = myHelloworld.GetConstructor(new Type[] { });

            object[] CBMyAttribute2s = myReflectConstructorInfo.GetCustomAttributes(true).Select(a => (object)a).ToArray();

            Assert.Equal(1, CBMyAttribute2s.Length);
            Assert.Equal(2, ((CBMyAttribute2)CBMyAttribute2s[0]).m_i);
        }
Example #8
0
        [Test] // SetCustomAttribute (CustomAttributeBuilder)
        public void SetCustomAttribute2()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, CallingConventions.Standard,
                new Type [1] {
                typeof(int)
            });

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

            TypeBuilder attrTb = module.DefineType("TestAttribute",
                                                   TypeAttributes.Public, typeof(Attribute));
            ConstructorBuilder attrCb = attrTb.DefineDefaultConstructor(
                MethodAttributes.Public);

            CustomAttributeBuilder cab = new CustomAttributeBuilder(
                attrCb, new object [0]);

            cb.SetCustomAttribute(cab);
            attrTb.CreateType();

            Type            emittedType = genClass.CreateType();
            ConstructorInfo ci          = emittedType.GetConstructor(
                new Type [1] {
                typeof(int)
            });

            Assert.IsNotNull(ci, "#1");
            object [] cas = ci.GetCustomAttributes(false);
            Assert.IsNotNull(cas, "#2");
            Assert.AreEqual(1, cas.Length, "#3");
            Assert.AreEqual("TestAttribute", cas [0].GetType().FullName, "#4");
        }
Example #9
0
 public static void SetCustomAttributes(ConstructorBuilder cb, IPersistentMap attributes)
 {
     foreach (CustomAttributeBuilder cab in CreateCustomAttributeBuilders(attributes))
     {
         cb.SetCustomAttribute(cab);
     }
 }
Example #10
0
        [Test] // GetCustomAttributes (Type, Boolean)
        public void GetCustomAttributes2_Incomplete()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, 0,
                new Type [1] {
                typeof(int)
            });

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

            Type            attrType = typeof(ObsoleteAttribute);
            ConstructorInfo ctorInfo =
                attrType.GetConstructor(new Type [] { typeof(String) });

            cb.SetCustomAttribute(new CustomAttributeBuilder(ctorInfo, new object [] { "FOO" }));

            try {
                cb.GetCustomAttributes(attrType, false);
                Assert.Fail("#1");
            } catch (NotSupportedException ex) {
                // The invoked member is not supported in a dynamic
                // module
                Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#2");
                Assert.IsNull(ex.InnerException, "#3");
                Assert.IsNotNull(ex.Message, "#4");
            }
        }
Example #11
0
        /// <summary>
        /// Generates the (<see cref="ScriptContext"/>, <see cref="DTypeDesc"/>) constructor.
        /// </summary>
        /// <param name="typeBuilder">The type builder.</param>
        /// <param name="shortConstructor">This type's short constructor.</param>
        /// <returns>The constructor builder.</returns>
        /// <remarks>
        /// The entire constructor is generated. See <see cref="PHP.Core.PhpObject(PHP.Core.ScriptContext,DTypeDesc)"/>.
        /// </remarks>
        public static ConstructorBuilder GenerateLongConstructor(TypeBuilder typeBuilder, ConstructorInfo shortConstructor)
        {
            ConstructorBuilder ctor_builder = typeBuilder.DefineConstructor(LongConstructorAttributes,
                                                                            CallingConventions.Standard, LongConstructorParamTypes);

            // annotate with EditorBrowsable attribute
#if !SILVERLIGHT // Not available on Silverlight
            ctor_builder.SetCustomAttribute(AttributeBuilders.EditorBrowsableNever);
#endif

            // define parameter names
            ctor_builder.DefineParameter(1, ParameterAttributes.None, "context");
            ctor_builder.DefineParameter(2, ParameterAttributes.None, "caller");

            // call this type's short constructor
            ILEmitter il = new ILEmitter(ctor_builder);
            il.Ldarg(FunctionBuilder.ArgThis);
            il.Ldarg(FunctionBuilder.ArgContextInstance);
            il.LdcI4(1);
            il.Emit(OpCodes.Call, shortConstructor);

            // call PhpObject.InvokeConstructor
            il.Ldarg(FunctionBuilder.ArgThis);
            il.Ldarg(1);
            il.Ldarg(2);
            il.Emit(OpCodes.Call, Methods.DObject_InvokeConstructor);

            il.Emit(OpCodes.Ret);

            return(ctor_builder);
        }
Example #12
0
        private static void GenerateConstructorSignatures(TypeBuilder tb, Type type)
        {
            var defaultCtor    = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
            var defaultCtorGen = defaultCtor.GetILGenerator();

            defaultCtorGen.Emit(OpCodes.Ret);

            var constructors = type.GetConstructors().Where(c => c.GetParameters().Any());

            foreach (var constructor in constructors)
            {
                var parameterTypes         = constructor.GetParameters().Select(p => p.ParameterType).ToArray();
                ConstructorBuilder builder = tb.DefineConstructor(constructor.Attributes, constructor.CallingConvention, parameterTypes);

                foreach (var attr in constructor.GetCustomAttributes(false).OfType <Attribute>())
                {
                    var cab = CreateAttributeBuilder(attr);
                    if (cab != null)
                    {
                        builder.SetCustomAttribute(cab);
                    }
                }

                var generator = builder.GetILGenerator();
                generator.Emit(OpCodes.Ret);
            }
        }
 protected virtual void GeneratingCustomAttribute(ConstructorBuilder constructorBuilder)
 {
     constructorBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(DynamicallyAttribute).GetConstructors().First(), EmptyArray <object> .Value));
     foreach (var attribute in _constructor.GetCustomAttributesData())
     {
         new ConstructorAttributeBuilder(constructorBuilder, attribute).Build();
     }
 }
Example #14
0
        /// <summary>
        /// Adds a <see cref="DebuggerHiddenAttribute"/> to a constructor.
        /// </summary>
        /// <param name="constructor">The constructor to add the attribute.</param>
        private static void AddDebuggerHiddenAttribute(ConstructorBuilder constructor)
        {
            Type attributeType = typeof(DebuggerHiddenAttribute);
            // ReSharper disable AssignNullToNotNullAttribute
            CustomAttributeBuilder attribute = new CustomAttributeBuilder(attributeType.GetConstructor(new Type[0]), new object[0]);

            // ReSharper restore AssignNullToNotNullAttribute
            constructor.SetCustomAttribute(attribute);
        }
        /// <summary>
        /// Applies an attribute to a constructor.
        /// </summary>
        /// <param name="constructor">An instance of ConstructorBuilder to apply the attribute to.</param>
        /// <param name="expression">An expression that represents the attribute.</param>
        public static void SetCustomAttribute(this ConstructorBuilder constructor, Expression <Func <Attribute> > expression)
        {
            var builder = constructor.Module.Assembly.ReflectionOnly
                              ? new ReflectionOnlyCustomAttributeBuilderBuilder()
                              : new CustomAttributeBuilderBuilder();
            var attribute = builder.Build(expression);

            constructor.SetCustomAttribute(attribute);
        }
        public void SetCustomAttribute_ConstructorBuilder_ByteArray()
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });

            ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });

            constructor.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });
        }
        public void SetCustomAttribute_NullCustomAtributeBuilder_ThrowsArgumentNullException()
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
            ILGenerator        ilGenerator = constructor.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ldarg_1);

            Assert.Throws <ArgumentNullException>("customBuilder", () => constructor.SetCustomAttribute(null));
        }
Example #18
0
        private static ConstructorInfo BuildConstructor(TypeBuilder type, FieldInfo helperClassFieldInfo)
        {
            // Declaring method builder
            // Method attributes
            ConstructorBuilder method = type.DefineConstructor(MethodAttributes.Public, 0, new[] { typeof(IDbConnection) });
            // Preparing Reflection instances
            ConstructorInfo ctor1 = typeof(DebuggerNonUserCodeAttribute).GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[] {
            },
                null
                );
            ConstructorInfo ctor2 = typeof(DataContextBase).GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[] {
                typeof(IDbConnection)
            },
                null
                );
            ConstructorInfo ctor3 = typeof(SqlDataContextHelperClass).GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[] {
                typeof(DataContext)
            },
                null
                );
            FieldInfo field4 = helperClassFieldInfo;

            // Adding custom attributes to method
            // [DebuggerNonUserCodeAttribute]
            method.SetCustomAttribute(
                new CustomAttributeBuilder(
                    ctor1,
                    new Type[] { }
                    )
                );
            // Parameter connection
            //ParameterBuilder connection = method.DefineParameter(0, ParameterAttributes.None, "connection");
            ILGenerator gen = method.GetILGenerator();

            // Writing body
            gen.Emit(OpCodes.Ldarg_0);
            gen.Emit(OpCodes.Ldarg_1);
            gen.Emit(OpCodes.Call, ctor2);
            gen.Emit(OpCodes.Ldarg_0);
            gen.Emit(OpCodes.Ldarg_0);
            gen.Emit(OpCodes.Newobj, ctor3);
            gen.Emit(OpCodes.Stfld, field4);
            gen.Emit(OpCodes.Ret);
            // finished
            return(method);
        }
Example #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConstructorBuilder"/> class
        /// with the specified parameters.
        /// </summary>
        /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</param>
        /// <param name="constructorBuilder">A <see cref="ConstructorBuilder"/></param>
        public ConstructorBuilderHelper(TypeBuilderHelper typeBuilder, ConstructorBuilder constructorBuilder)
            : base(typeBuilder)
        {
            if (constructorBuilder == null)
            {
                throw new ArgumentNullException("constructorBuilder");
            }

            _constructorBuilder = constructorBuilder;
            _constructorBuilder.SetCustomAttribute(Type.Assembly.BLToolkitAttribute);
        }
 public void SetAttribute(CustomAttributeBuilder attribute)
 {
     if (constructorBuilder == null)
     {
         methodBuilder.SetCustomAttribute(attribute);
     }
     else
     {
         constructorBuilder.SetCustomAttribute(attribute);
     }
 }
Example #21
0
        [Test] // SetCustomAttribute (ConstructorInfo, Byte [])
        public void SetCustomAttribute1()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                0, 0, new Type [1] {
                typeof(int)
            });

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

            byte[] custAttrData = { 1, 0, 0, 0, 0 };
            Type   attrType     = Type.GetType
                                      ("System.Reflection.AssemblyKeyNameAttribute");

            Type[] paramTypes = new Type[1];
            paramTypes[0] = typeof(String);
            ConstructorInfo ctorInfo =
                attrType.GetConstructor(paramTypes);

            cb.SetCustomAttribute(ctorInfo, custAttrData);

            // Null arguments again
            try {
                cb.SetCustomAttribute(null, new byte [2]);
                Assert.Fail("#B1");
            } catch (ArgumentNullException ex) {
                Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#B2");
                Assert.IsNull(ex.InnerException, "#B3");
                Assert.IsNotNull(ex.Message, "#B4");
                Assert.AreEqual("con", ex.ParamName, "#B5");
            }

            try {
                cb.SetCustomAttribute(ctorInfo, null);
                Assert.Fail("#C1");
            } catch (ArgumentNullException ex) {
                Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#C2");
                Assert.IsNull(ex.InnerException, "#C3");
                Assert.IsNotNull(ex.Message, "#C4");
                Assert.AreEqual("binaryAttribute", ex.ParamName, "#C5");
            }
        }
Example #22
0
        public void TestThrowsExceptionOnNullConstructorInfo()
        {
            AssemblyName    TestAssemblyName    = new AssemblyName("TestAssembly");
            AssemblyBuilder TestAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);

            ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssemblyBuilder, "Module1");

            TypeBuilder        TestTypeBuilder        = TestModuleBuilder.DefineType("TestTypeBuilder", TypeAttributes.Public);
            ConstructorBuilder TestConstructorBuilder =
                TestTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });

            Assert.Throws <ArgumentNullException>(() => { TestConstructorBuilder.SetCustomAttribute(null, new byte[] { 01, 00, 05, 00, 00, 00 }); });
        }
Example #23
0
        protected virtual void DefineConstructor()
        {
            ConstructorBuilder constructorBuilder = TypeBuilder.DefineConstructor(
                MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.Standard, Type.EmptyTypes);

            constructorBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                                      typeof(DebuggerHiddenAttribute).GetConstructor(Type.EmptyTypes), new object[0]));

            ILGenerator il = constructorBuilder.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
            il.Emit(OpCodes.Ret);
        }
Example #24
0
        public void TestThrowsExceptionOnNullCustomAttributeBuilder()
        {
            AssemblyName    myAssemblyName  = new AssemblyName("EmittedAssembly");
            AssemblyBuilder myAssembly      = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
            ModuleBuilder   myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssembly, "Module1");

            TypeBuilder        myTypeBuilder = myModuleBuilder.DefineType("HelloWorld", TypeAttributes.Public);
            ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
                MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
            ILGenerator myILGenerator = myConstructor.GetILGenerator();

            myILGenerator.Emit(OpCodes.Ldarg_1);
            Assert.Throws <ArgumentNullException>(() => { myConstructor.SetCustomAttribute(null); });
        }
Example #25
0
        public void TestSetCustomAttribute()
        {
            AssemblyName    TestAssemblyName    = new AssemblyName("TestAssembly");
            AssemblyBuilder TestAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);

            ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssemblyBuilder, "Module1");

            TypeBuilder        TestTypeBuilder        = TestModuleBuilder.DefineType("TestTypeBuilder", TypeAttributes.Public);
            ConstructorBuilder TestConstructorBuilder =
                TestTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
            Type            myType            = typeof(CBMyAttribute1);
            ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[] { typeof(int) });

            TestConstructorBuilder.SetCustomAttribute(myConstructorInfo, new byte[] { 01, 00, 05, 00, 00, 00 });
        }
    private static Type MyCreateCallee(AppDomain domain)
    {
        AssemblyName myAssemblyName = new AssemblyName();

        myAssemblyName.Name = "EmittedAssembly";
        // Define a dynamic assembly in the current application domain.
        AssemblyBuilder myAssembly =
            domain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
        // Define a dynamic module in this assembly.
        ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
        // Construct a 'TypeBuilder' given the name and attributes.
        TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
                                                               TypeAttributes.Public);
        // Define a constructor of the dynamic class.
        ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
            MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(String) });
        ILGenerator myILGenerator = myConstructor.GetILGenerator();

        myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
        myILGenerator.Emit(OpCodes.Ldarg_1);
        MethodInfo myMethodInfo =
            typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });

        myILGenerator.Emit(OpCodes.Call, myMethodInfo);
        myILGenerator.Emit(OpCodes.Ret);
        Type            myType            = typeof(MyAttribute);
        ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[2] {
            typeof(String), typeof(int)
        });
        CustomAttributeBuilder attributeBuilder =
            new CustomAttributeBuilder(myConstructorInfo, new object[2] {
            "Hello", 2
        });

        try
        {
            myConstructor.SetCustomAttribute(attributeBuilder);
        }
        catch (ArgumentNullException ex)
        {
            Console.WriteLine("The following exception has occurred : " + ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("The following exception has occurred : " + ex.Message);
        }
        return(myTypeBuilder.CreateType());
    }
Example #27
0
 internal void CompileBaseDefinition(TypeBuilder typeBuilder)
 {
     VerificationBaseDefinition(true);
     Type[] parameters = (this.parameters == null) ? Type.EmptyTypes : this.parameters.Select(m => (Type)m).ToArray();
     constructorBuilder = typeBuilder.DefineConstructor(methodAttributes, CallingConventions.Standard, parameters);
     foreach (ParameterCreator parameter in configurationParameter)
     {
         parameter.Compile(constructorBuilder);
     }
     foreach (CustomAttributeBuilder customAttribute in customAttributes)
     {
         constructorBuilder.SetCustomAttribute(customAttribute);
     }
     constructorInfo = constructorBuilder;
     State           = Metadata.State.BaseDefinition;
 }
        public void SetCustomAttribute_ConstructorBuilder_ByteArray()
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });

            ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });

            constructor.SetCustomAttribute(attributeConstructor, new byte[] { 1, 0, 5, 0, 0, 0 });
            constructor.GetILGenerator().Emit(OpCodes.Ret);

            Type            createdType        = type.CreateType();
            ConstructorInfo createdConstructor = createdType.GetConstructor(new Type[] { typeof(int) });

            Attribute[]     attributes = createdConstructor.GetCustomAttributes().ToArray();
            IntAllAttribute attribute  = Assert.IsType <IntAllAttribute>(attributes[0]);

            Assert.Equal(5, attribute._i);
        }
Example #29
0
        public void GetCustomAttributes_Emitted()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, 0,
                new Type [1] {
                typeof(int)
            });

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

            Type            attrType = typeof(ObsoleteAttribute);
            ConstructorInfo ctorInfo =
                attrType.GetConstructor(new Type [] { typeof(String) });

            cb.SetCustomAttribute(new CustomAttributeBuilder(ctorInfo, new object [] { "FOO" }));

            Type t = genClass.CreateType();

            // Try the created type
            {
                ConstructorInfo ci    = t.GetConstructors() [0];
                object[]        attrs = ci.GetCustomAttributes(true);

                Assert.AreEqual(1, attrs.Length, "#A1");
                Assert.IsTrue(attrs [0] is ObsoleteAttribute, "#A2");
                Assert.AreEqual("FOO", ((ObsoleteAttribute)attrs [0]).Message, "#A3");
            }

            // Try the type builder
            {
                ConstructorInfo ci    = genClass.GetConstructors() [0];
                object[]        attrs = ci.GetCustomAttributes(true);

                Assert.AreEqual(1, attrs.Length, "#B1");
                Assert.IsTrue(attrs [0] is ObsoleteAttribute, "#B2");
                Assert.AreEqual("FOO", ((ObsoleteAttribute)attrs [0]).Message, "#B3");
            }
        }
Example #30
0
        /// <summary>
        /// Defines the (<see cref="ScriptContext"/>) constructor.
        /// </summary>
        /// <param name="typeBuilder">The type builder.</param>
        /// <returns>The constructor builder.</returns>
        /// <remarks>
        /// Part of the constructor body - containing a call to parent's short constructor - is generated.
        /// At least an <see cref="OpCodes.Ret"/> has to be emitted to make the constructor complete.
        /// </remarks>
        public static ConstructorBuilder DefineShortConstructor(TypeBuilder typeBuilder)
        {
            ConstructorBuilder ctor_builder = typeBuilder.DefineConstructor(ShortConstructorAttributes,
                                                                            CallingConventions.Standard, ShortConstructorParamTypes);

            // annotate with EditorBrowsable attribute
#if !SILVERLIGHT // Not available on Silverlight
            ctor_builder.SetCustomAttribute(AttributeBuilders.EditorBrowsableNever);
#endif

            // define parameter names
            ctor_builder.DefineParameter(1, ParameterAttributes.None, "context");
            ctor_builder.DefineParameter(2, ParameterAttributes.None, "newInstance");

            // call the base type's short constructor
            ILEmitter il = new ILEmitter(ctor_builder);
            il.Ldarg(FunctionBuilder.ArgThis);
            il.Ldarg(FunctionBuilder.ArgContextInstance);
            il.Ldarg(2);
            il.Emit(OpCodes.Call, typeBuilder.BaseType.GetConstructor(ShortConstructorParamTypes));

            return(ctor_builder);
        }
				internal override void Apply(ClassLoaderWrapper loader, ConstructorBuilder cb, object annotation)
				{
					if (type.IsSubclassOf(Types.SecurityAttribute))
					{
#if STATIC_COMPILER
						cb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#elif STUB_GENERATOR
#else
						SecurityAction action;
						PermissionSet permSet;
						if (MakeDeclSecurity(type, annotation, out action, out permSet))
						{
							cb.AddDeclarativeSecurity(action, permSet);
						}
#endif
					}
					else
					{
						cb.SetCustomAttribute(MakeCustomAttributeBuilder(loader, annotation));
					}
				}