MSIL实用指南-给字段、属性、方法、类、程序集加Attribute

 

C#编程中可以给字段、方法、类以及程序集加特性即继承于Attribute的类。这里讲解怎么在IL中给
它们加上特性。

生成字段的对应的类是FieldBuilder,生成属性的对应的类是PropertyBuilder,生成方法的对应的
类是MethodBuilder,生成类型的对应的类是TypeBuilder,生成程序集的对应的类是AssemblyBuilder。
这些类都有一个共同的方法SetCustomAttribute,而且参数都是一样的,具体是
SetCustomAttribute(CustomAttributeBuilder customBuilder)

我们可以用这个方法给它们添加特性。

步骤1:用反射获得特性的ConstructorInfo
比如

Type attrType = typeof(SerializableAttribute);
ConstructorInfo infoConstructor = attrType.GetConstructor(new Type[] { });

步骤2:创建一个CustomAttributeBuilder
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor,<infoConstructor参数数组>);

步骤3:调用SetCustomAttribute方法
上面说的FieldBuilder等等都是有同样的SetCustomAttribute方法,所以调用程序也是一样。

完成的程序如下

 

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace LX1_ILDemo
{
    public class Demo20_CustomAttribute
    {
        static string binaryName = "Demo20_CustomAttribute.dll";
        static string namespaceName = "LX1_ILDemo";
        static string typeName = "CustomAttributeDemo";

        static AssemblyBuilder assemblyBuilder;
        static ModuleBuilder moduleBuilder;
        static TypeBuilder typeBuilder;

        public static void Generate()
        {
            InitAssembly();
            typeBuilder = moduleBuilder.DefineType(namespaceName + "." + typeName, TypeAttributes.Public | TypeAttributes.Abstract);
            Generate_Field();
            Generate_Property();
            Generate_Method();
            TypeAddAttr();
            AssemblyAddAttr();
            SaveAssembly();
            Console.WriteLine("生成成功");
        }

        static void TypeAddAttr()
        {
            Type attrType = typeof(SerializableAttribute);
            ConstructorInfo infoConstructor = attrType.GetConstructor(new Type[] { });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { });
            typeBuilder.SetCustomAttribute(attributeBuilder);
        }

        static void AssemblyAddAttr()
        {
            Type myType = typeof(AssemblyCopyrightAttribute);
            ConstructorInfo infoConstructor = myType.GetConstructor(new Type[] { typeof(string) });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { "IL Test Copyright" });
            assemblyBuilder.SetCustomAttribute(attributeBuilder);
        }

        static void Generate_Method()
        {
            Type attrType = typeof(System.ObsoleteAttribute);
            ConstructorInfo infoConstructor = attrType.GetConstructor(new Type[] { });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { });

            MethodBuilder methodBuilder = typeBuilder.DefineMethod("Test",
                MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
                typeof(void), new Type[] { });

            methodBuilder.SetCustomAttribute(attributeBuilder);
        }

        static void Generate_Field()
        {
            Type attrType = typeof(System.ObsoleteAttribute);
            ConstructorInfo infoConstructor = attrType.GetConstructor(new Type[] { });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { });
            FieldBuilder fieldBuilder = typeBuilder.DefineField("Name", typeof(string), FieldAttributes.Private);
            fieldBuilder.SetCustomAttribute(attributeBuilder);
        }

        static void Generate_Property()
        {
            FieldBuilder fieldBuilder = typeBuilder.DefineField
                 ("_Area", typeof(string), FieldAttributes.Private);

            PropertyBuilder propertyBuilder = typeBuilder.DefineProperty
                ("Area", PropertyAttributes.HasDefault, typeof(string), null);

            MethodAttributes getSetAttr =
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            MethodBuilder _methodBuilder =
                typeBuilder.DefineMethod("get_Area", getSetAttr, typeof(string), Type.EmptyTypes);

            ILGenerator custNameGetIL = _methodBuilder.GetILGenerator();

            custNameGetIL.Emit(OpCodes.Ldarg_0);
            custNameGetIL.Emit(OpCodes.Ldfld, fieldBuilder);
            custNameGetIL.Emit(OpCodes.Ret);
            propertyBuilder.SetGetMethod(_methodBuilder);

            Type attrType = typeof(ObsoleteAttribute);
            ConstructorInfo infoConstructor = attrType.GetConstructor(new Type[] { });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { });
            propertyBuilder.SetCustomAttribute(attributeBuilder);
        }

        static void InitAssembly()
        {
            AssemblyName assemblyName = new AssemblyName(namespaceName);
            assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, binaryName);
        }

        static void SaveAssembly()
        {
            Type t = typeBuilder.CreateType(); //完成Type,这是必须的
            assemblyBuilder.Save(binaryName);
        }
    }
}
View Code

 

posted @ 2018-04-04 14:09  Z语言  阅读(220)  评论(0编辑  收藏  举报