MSIL实用指南-创建字段
本篇讲解怎么创建字段,主要是在修饰符的创建上。
创建字段的方法是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); } } }