本篇讲解生成构造函数的一些知识,包括创建实例构造函数、静态构造函数、调用父类构造函数。

生成构造函数的方法
生成构造函数的方法是TypeBuilder.DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes),调用它返回一个ConstructorBuilder对象。

实例:

ConstructorBuilder newBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, new Type[]{ typeof(String)});

生成静态构造函数
按照C#语言规范,静态构造函数是static和private,并且没有参数,所以它的方法属性是
MethodAttributes.Private|MethodAttributes.Static。
实例:

ConstructorBuilder myConstructorBuilder = typeBuilder.DefineConstructor(
MethodAttributes.Private|MethodAttributes.Static,
CallingConventions.Standard, new Type[] { });

调用基类构造函数
实例构造函数都要调用父类的构造函数,
它需要第一步传入this作为参数,
第二步传入父类构造函数所需要的参数,
第三步调用父类构造函数。
实例:

ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[]{}));

完整程序如下:

using System;
using System.Reflection;
using System.Reflection.Emit; namespace LX1_ILDemo
{
class Demo13_Constructor
{
static string binaryName = "Demo13_Constructor.dll";
static string namespaceName = "LX1_ILDemo";
static string typeName = "ConstructorDemo"; static AssemblyBuilder assemblyBuilder;
static ModuleBuilder moduleBuilder;
static TypeBuilder typeBuilder; private static void Generate_Constructor1()
{
ConstructorBuilder newBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, new Type[]{ typeof(String)});
ILGenerator ilGenerator = newBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[]{}));
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }));
ilGenerator.Emit(OpCodes.Ret);
} private static void Generate_Constructor2()
{
ConstructorBuilder myConstructorBuilder = typeBuilder.DefineConstructor(
MethodAttributes.Private|MethodAttributes.Static,
CallingConventions.Standard, new Type[] { });
ILGenerator ilGenerator = myConstructorBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldstr, "Static Constructor");
ilGenerator.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }));
ilGenerator.Emit(OpCodes.Ret);
} public static void Generate()
{
InitAssembly(); typeBuilder = moduleBuilder.DefineType( namespaceName+"."+ typeName, TypeAttributes.Public);
Generate_Constructor1();
Generate_Constructor2();
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实用指南-生成索引器

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

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

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

  3. MSIL实用指南-生成接口

    本篇讲解怎么样生成接口,即interface. 一.创建类型创建一个接口类型依旧用ModuleBuilder的DefineType方法,但是它的第二个参数必须要有TypeAttributes.Inte ...

  4. MSIL实用指南-生成if...else...语句

    if...else...语句是非常重要的选择语句,它的生成一般需要ILGenerator的DefineLabel方法和MarkLabel方法,以及Brtrue_S和Br_S指令. 一.DefineLa ...

  5. MSIL实用指南-生成内部类

    生成内部类用TypeBuilder的DefineNestedType方法,得到另一个TypeBuilder.内部类的可访问性都是TypeAttributes的“Nested”开头一些成员.实例代码:y ...

  6. MSIL实用指南-生成foreach语句

    foreach可以迭代数组或者一个集合对象.foreach语句格式是它的生成步骤是foreach (<成员> in <集合>) <循环体> 一.声明三个变量,loc ...

  7. MSIL实用指南-生成for语句

    for语句格式是这样的for(<初始化语句>;<条件语句>;<自增减语句>) <循环体> 它可以转换为while语句 if(<条件语句>){ ...

  8. MSIL实用指南-生成异常处理

    本篇讲解怎么生成异常.C# 异常处理时建立在四个关键词之上的:try.catch.finally 和 throw. 一.异常的抛出抛出异常在C#语言中要使用throw关键字,使用方法是throw &l ...

  9. MSIL实用指南-生成while语句

    本篇讲解怎样生成while语句.while语句是编程语言中很重要的循环语句,它的结构是while(<表达式>) <语句或语句块> 当表达式的结果为true时就一直执行语句或语句 ...

随机推荐

  1. DM6446 uboot分析

    1. 顶层目录下的Makefile 按照配置顺序: davinci_config :    unconfig @./mkconfig $(@:_config=) arm arm926ejs davin ...

  2. R︱高效数据操作——data.table包(实战心得、dplyr对比、key灵活用法、数据合并)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于业务中接触的数据量很大,于是不得不转战开始 ...

  3. 基于LDA的Topic Model变形

    转载于: 转:基于LDA的Topic Model变形 最近有想用LDA理论的变形来解决问题,调研中.... 基于LDA的Topic Model变形 基于LDA的Topic Model变形最近几年来,随 ...

  4. freemarker报错之十

    1.错误描述 <html> <head> <meta http-equiv="content-type" content="text/htm ...

  5. 将泛类型集合List类转换成DataTable

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  6. 使用Python收集获取Linux系统主机信息

    爬虫代理IP由芝麻HTTP服务供应商提供 使用 python 代码收集主机的系统信息,主要:主机名称.IP.系统版本.服务器厂商.型号.序列号.CPU信息.内存等系统信息. #!/usr/bin/en ...

  7. Jupyter notebook Tensorflow GPU Memory 释放

    Jupyter notebook 每次运行完tensorflow的程序,占着显存不释放.而又因为tensorflow是默认申请可使用的全部显存,就会使得后续程序难以运行.暂时还没有找到在jupyter ...

  8. 网络协议笔记-网络层-ARP协议

    [2-地址解析协议ARP] [2.1-基本概念] 地址解析协议ARP的作用是根据主机的IP地址,找出该主机的硬件地址. [2.2-为什么要使用ARP] 在数据传输过程中,网络层使用的IP地址,但是在实 ...

  9. java将字符串转换为指定的时间格式

    *String dateString = "18:31:43";    try {     Date date = new SimpleDateFormat("HH:mm ...

  10. KMP算法 Next数组详解

    题面 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百 ...