本篇讲解怎么创建字段,主要是在修饰符的创建上。

创建字段的方法是TypeBuilder.DefineField,传入字段名称、字段类型、字段修饰符等参数,返回一个FieldBuilder对象。
先看这一句程序

FieldBuilder nameFieldBuilder = typeBuilder.DefineField("Name", typeof(string), FieldAttributes.Private);

上面一条程序是创建一个叫"Name"的数据类型为"string"的字段,等同于

private string Name;

FieldAttributes是枚举类型,决定字段的修饰符。详细说明如下

C#语言规范只是用到了FieldAttributes的一部分枚举,并没有用到全部。

FieldBuilder ageFieldBuilder = typeBuilder.DefineField("Arg", typeof(int), FieldAttributes.Public);

等同于C#语言
public int Arg;

FieldBuilder maxCountFieldBuilder = typeBuilder.DefineField("MaxCount", typeof(long), FieldAttributes.Family);

等同于C#语言
protected long MaxCount;

FieldBuilder IFFieldBuilder = typeBuilder.DefineField("IF", typeof(string), FieldAttributes.Assembly);

等同于C#语言
internal string IF;

FieldBuilder PIFFieldBuilder = typeBuilder.DefineField("PIF", typeof(string),
FieldAttributes.Family | FieldAttributes.FamORAssem);

等同于C#语言
protected internal string PIF;

FieldBuilder PRFieldBuilder = typeBuilder.DefineField("RF", typeof(string),
FieldAttributes.Private | FieldAttributes.InitOnly);

等同于C#语言
private readonly string RF;

FieldBuilder CFFieldBuilder = typeBuilder.DefineField("CF", typeof(string),
FieldAttributes.Private | FieldAttributes.Literal);

等同于C#语言
private const string CF;

FieldBuilder SFFieldBuilder = typeBuilder.DefineField("SF", typeof(string),
FieldAttributes.Private | FieldAttributes.Static);

等同于C#语言
private static string SF;

完成的程序如下:

using System;
using System.Reflection;
using System.Reflection.Emit; namespace LX1_ILDemo
{
class Demo05_FieldCreate
{
static string binaryName = "Demo05_FieldCreate.dll";
static string namespaceName = "LX1_ILDemo";
static string typeName = "EmitFieldCreate"; static AssemblyBuilder assemblyBuilder;
static ModuleBuilder moduleBuilder;
static TypeBuilder typeBuilder; static void Create_Fileds()
{
/* private string Name; */
FieldBuilder nameFieldBuilder = typeBuilder.DefineField("Name", typeof(string), FieldAttributes.Private);
/* public int Arg; */
FieldBuilder ageFieldBuilder = typeBuilder.DefineField("Arg", typeof(int), FieldAttributes.Public);
/* protected long MaxCount; */
FieldBuilder maxCountFieldBuilder = typeBuilder.DefineField("MaxCount", typeof(long), FieldAttributes.Family); /* internal string IF; */
FieldBuilder IFFieldBuilder = typeBuilder.DefineField("IF", typeof(string), FieldAttributes.Assembly); /* protected internal string PIF; */
FieldBuilder PIFFieldBuilder = typeBuilder.DefineField("PIF", typeof(string),
FieldAttributes.Family | FieldAttributes.FamORAssem); /* private readonly string RF; */
FieldBuilder PRFieldBuilder = typeBuilder.DefineField("RF", typeof(string),
FieldAttributes.Private | FieldAttributes.InitOnly); /* private const string CF; */
FieldBuilder CFFieldBuilder = typeBuilder.DefineField("CF", typeof(string),
FieldAttributes.Private | FieldAttributes.Literal); /* private static string SF; */
FieldBuilder SFFieldBuilder = typeBuilder.DefineField("SF", typeof(string),
FieldAttributes.Private | FieldAttributes.Static);
} public static void Generate()
{
InitAssembly(); typeBuilder = moduleBuilder.DefineType(namespaceName + "." + typeName, TypeAttributes.Public);
Create_Fileds(); SaveAssembly();
Console.WriteLine("生成成功");
} 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实用指南-创建字段的更多相关文章

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

    C#编程中可以给字段.方法.类以及程序集加特性即继承于Attribute的类.这里讲解怎么在IL中给它们加上特性. 生成字段的对应的类是FieldBuilder,生成属性的对应的类是PropertyB ...

  2. MSIL实用指南-创建枚举类型

    创建枚举类型比较简单,主要使用moduleBuilder.DefineEnum 和enumBuilder.DefineLiteral. 第一步:创建 EnumBuilder 创建 EnumBuilde ...

  3. MSIL实用指南-创建方法和定义参数

    本篇讲解实现创建方法.指定参数的名称.实现参数加out和ref修饰符.以及参数加默认值. 创建方法 创建方法用类TypeAttributes的 DefineMethod(string name, Me ...

  4. MSIL实用指南-生成索引器

    MSIL实用指南-生成索引器 索引器是一种特殊的属性,它有参数的,也有get和set方法,属性名称一般是"Item",并且方法名称一般名称是"get_Item" ...

  5. MSIL实用指南-Action的生成和调用

    MSIL实用指南-Action的生成和调用 System.Action用于封装一个没有参数没有返回值的方法.这里生成需要Ldftn指令. 下面讲解怎生成如下的程序. class ActionTest ...

  6. MSIL实用指南-闭包的生成和调用

    闭包(Closure)是词法闭包(Lexical Closure)的简称.对闭包的具体定义有很多种说法,这些说法大体可以分为两类: 一种说法认为闭包是符合一定条件的函数,比如参考资源中这样定义闭包:闭 ...

  7. MSIL实用指南-字段的加载和保存

    字段有静态字段和非静态字段之分,它们的加载保存指令也是不一样的,并且非静态字段要生成this. 静态字段的加载加载静态字段的指令是Ldsfld.ilGenerator.Emit(OpCodes.Lds ...

  8. MSIL实用指南-生成属性

    本篇讲解怎么生成属性,包括get和set方法. 第一步,生成一个字段生成字段用TypeBuilder.DefineField方法.实例代码: FieldBuilder customerNameBldr ...

  9. MSIL实用指南-局部变量的声明、保存和加载

    这一篇讲解方法内的局部变量是怎么声明.怎样保存.怎样加载的. 声明局部变量声明用ILGenerator的DeclareLocal方法,参数是局部变量的数据类型,得到一个局部变量对应的创建类LocalB ...

随机推荐

  1. entity framework core在独立类库下执行迁移操作

    之前学习EFCore的时候,都是在VS创建的默认模板里面进行的,按照官方文档,直接就可以搞定. 今天新项目准备上.Net Core,打算先按照国际惯例,进行分层,数据访问层是用EFCore来做,于是就 ...

  2. Jquery就是这么简单

    什么是Jquery? Jquey就是一款跨主流浏览器的JavaScript库,简化JavaScript对HTML操作 就是封装了JavaScript,能够简化我们写代码的一个JavaScript库 为 ...

  3. Batch Normalization&Dropout浅析

    一. Batch Normalization 对于深度神经网络,训练起来有时很难拟合,可以使用更先进的优化算法,例如:SGD+momentum.RMSProp.Adam等算法.另一种策略则是高改变网络 ...

  4. static_cast, dynamic_cast, const_cast 类型转换如何使用?

    static_cast 用法:static_cast < type-id > ( expression ) 说明:该运算符把expression转换为type-id类型,但没有运行时类型检 ...

  5. 《Java编程思想》读书笔记

    前言 这个月一直没更新,就是一直在读这本<Java编程思想>,这本书可以在Java业界被传神的一本书,无论谁谈起这本书都说好,不管这个人是否真的读过这本书,都说啊,这本书很好.然后再看这边 ...

  6. yaf框架学习文件配置

    文件配置: 在配置php支持yaf的时候,可以设置一个参数yaf.environ:把本地开发设置成develop.测试环境配置成test.生产环境配置成product. [yaf] extension ...

  7. CentOS7网卡命名规则

    CentOS6之前基于传统的命名方式如:eth1,eth0.... Centos7提供了不同的命名规则,默认是基于固件.拓扑.位置信息来分配.这样做的优点是命名是全自动的.可预知的,缺点是比eth0. ...

  8. JAVA代码发送邮件示例和解释(二)

    之前有使用过一次在程序中发邮件的功能,也写了一篇相关的笔记,当时使用的是163邮箱,经过测试多个163账号都是可行的.但是最近把账号换成中国移动@chinamobilesz.com这种结尾的时候,却一 ...

  9. 【php】curl常见的错误号和解释

    curl 错误代码列表 CURLE_UNSUPPORTED_PROTOCOL (1) – 您传送给 libcurl 的网址使用了此 libcurl 不支持的协议. 可能是您没有使用的编译时选项造成了这 ...

  10. Android线程间通信机制——深入理解 Looper、Handler、Message

    在Android中,经常使用Handler来实现线程间通信,必然要理解Looper , Handler , Message和MessageQueue的使用和原理,下面说一下Looper , Handl ...