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);
}
}
}
MSIL实用指南-给字段、属性、方法、类、程序集加Attribute的更多相关文章
- MSIL实用指南-创建字段
本篇讲解怎么创建字段,主要是在修饰符的创建上. 创建字段的方法是TypeBuilder.DefineField,传入字段名称.字段类型.字段修饰符等参数,返回一个FieldBuilder对象.先看这一 ...
- MSIL实用指南-生成索引器
MSIL实用指南-生成索引器 索引器是一种特殊的属性,它有参数的,也有get和set方法,属性名称一般是"Item",并且方法名称一般名称是"get_Item" ...
- c#基础3-方法的重载静态和非静态,字段属性,方法
方法的重载概念:方法的重载指的就是方法的名称相同给,但是参数不同.参数不同,分为两种情况1).如果参数的个数相同,那么参数的类型就不能相同.2).如果参数的类型相同,那么参数的个数就不能相同.***方 ...
- MSIL实用指南-Action的生成和调用
MSIL实用指南-Action的生成和调用 System.Action用于封装一个没有参数没有返回值的方法.这里生成需要Ldftn指令. 下面讲解怎生成如下的程序. class ActionTest ...
- C#-类 字段 属性 方法 特性
方法的签名 包括参数个数.参数类型.方法返回值 base和this关键字 1.使用base关键字,就可以使用父类中的字段.属性.方法 2.一旦父类中存在有参数的构造函数,那么子类就必须使用base来覆 ...
- MSIL实用指南-生成属性
本篇讲解怎么生成属性,包括get和set方法. 第一步,生成一个字段生成字段用TypeBuilder.DefineField方法.实例代码: FieldBuilder customerNameBldr ...
- MSIL实用指南-struct的生成和操作
struct(结构)是一种值类型,用于将一组相关的信息变量组织为一个单一的变量实体.所有的结构都继承自System.ValueType类,因此是一种值类型,也就是说,struct实例分配在线程的堆栈( ...
- MSIL实用指南-创建方法和定义参数
本篇讲解实现创建方法.指定参数的名称.实现参数加out和ref修饰符.以及参数加默认值. 创建方法 创建方法用类TypeAttributes的 DefineMethod(string name, Me ...
- MSIL实用指南-字段的加载和保存
字段有静态字段和非静态字段之分,它们的加载保存指令也是不一样的,并且非静态字段要生成this. 静态字段的加载加载静态字段的指令是Ldsfld.ilGenerator.Emit(OpCodes.Lds ...
随机推荐
- 分享一个 Linux 环境下,强力的Python 小工具
场景 Linux 用户,经常需要在终端查看一些数据,从文件里看 或者网络协议获取数据并查看. 比如,查看文件里的json数据:比如,查看etcd里存下的数据. 如果直接看cat 或者 curl 得到的 ...
- 在pom.xml中的dependencies点击add怎么没有搜索到相关jar包
1.eclipse菜单 window-> show view –> other –> Maven 2.在打开的窗口里,右键 local repositories –> loca ...
- Oracle 统计信息介绍
统计信息自动执行需要以下条件满足: dba_autotask_task 字段status值ENABLED dba_autotask_client 字段status值ENABLED dba_auto ...
- context创建过程解析(一)之deployDescriptors
总结:主要是创建Context对象,并且将默认context配置,host级别配置,context配置的值设置进去,设置docBase,如果是war包就解压到webapp的目录中,重新设置docBas ...
- 又一个轮子--QMapper
1 前言 我喜欢造轮子,一是造的时候就是深刻学习的时候,二是造着造着,说不定某天比世面上的其它轮子都要好呢.比如造过Networksocket,也造过WebApiClient,现在我也要造一个Mapp ...
- windows server2008下搭建ftp服务
在工作中不光使用linux系统下的ftp服务,也得使用windows下的,今天领导让我做一个,踩了很多坑,终于是做完了,重现下过程,我们就来一步一步搭建我们的windows下的ftp服务器: 1.环境 ...
- 10个常用的linux的命令
以下就是今天我们要介绍的Linux命令: man touch, cat and less sort and grep cut sed tar find diff uniq chmo ...
- 五、Python基础(2)
五,Python基础(2) 1.数据类型基础 (一)什么是数据类型? 用于区分变量值的不同类型. (二)为何对数据分类? 针对不同状态就应该用不同类型的数据去标识. (三)数据类型分类 1.数字类型 ...
- 在Java大环境下.NET程序员如何夺得一线生机
先来看一组数据,从某招聘网站直接检索3-4w的岗位,会看到Java与.NET社会需求量的巨大差异,这里就不再对比高薪的岗位了,.NET的高薪岗位更是少的可怜: 笔者从业十余年,一直是在.NET圈子 ...
- 【原创】JAVA进程突然消失的原因?
引言 值此七夕佳节,烟哥放弃了无数妹纸的邀约,坐在电脑面前码字,就是为了给读者带来新的知识,这是一件伟大的事业! 好吧,实际情况是没人约.为了化解尴尬,我决定卖力写文章,嗯,一定是我过于屌丝! 好了, ...