MSIL实用指南-生成构造函数
本篇讲解生成构造函数的一些知识,包括创建实例构造函数、静态构造函数、调用父类构造函数。
生成构造函数的方法
生成构造函数的方法是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); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?