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); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?