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

创建字段的方法是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. Egret学习笔记 (Egret打飞机-4.添加主角飞机和实现飞行效果)

    今天继续写点击了开始之后,添加一个飞机到场景中,然后这个飞机的尾巴还在冒火的那种感觉 先拆解一下步骤 1.首先完成飞机容器的图片加载 2.然后把容器添加到场景中 3.然后实现动画 -首先,我们新建一个 ...

  2. 买帽子 (hash)

    思路:表示数字i出现的次数,在输入的同时记录每个数字出现的次数.最后从0枚举到1000判断第三个是否存在,存在则记录该数字. #include <stdio.h> #include < ...

  3. windows NLB实现MSSQL读写分离--从数据库集群读负载均衡

    主从模式,几乎大部分出名的数据库都支持的一种集群模式. 当Web站点的访问量上去之后,很多站点,选择读写分离,减轻主数据库的的压力.当然,一主多从也可以作用多个功能,比如备份.这里主要演示如何实现从数 ...

  4. 关于使用srping @RequestParam 容易出错的地方

    大家都知道,在spring中的@RequestParam主要用户传递参数用的,具体的解释就是将请求参数去数据映射到功能处理方法的参数上.其中包括三个参数: value:参数名字,即入参的请求参数名字, ...

  5. Hbase多列范围查找(效率)

    Hbase索引表的结构 Hbase Rowkey 设计 Hbase Filter Hbase二级索引 Hbase索引表的结构 在HBase中,表格的Rowkey按照字典排序,Region按照RowKe ...

  6. uboot 添加hello命令

    平台:MPC8315(POWERPC) 1.在/common/ 目录下创建自己的文件,最好前缀为cmd_. cmd_hello.c ********************************** ...

  7. 不同WINDOWS平台下磁盘逻辑扇区的直接读写

    不同WINDOWS平台下磁盘逻辑扇区的直接读写 关键字:VWIN32.中断.DeviceIoControl 一.概述 在DOS操作系统下,通过BIOS的INT13.DOS的INT25(绝对读).INT ...

  8. Struts 有哪些常用标签库

    Struts 有哪些常用标签库 1.html标签库 2.bean标签库 3.logic标签库

  9. 利用ICSharpCode.SharpZipLib进行压缩

    #ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is im ...

  10. 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法

    在我编程中,我遇到了一个这样的报错, 可是我引用的product类中又确实定义了这么一个方法, protected void BindPageData(int categoryID) { Produc ...