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

 

生成构造函数的方法
生成构造函数的方法是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);
        }
    }
}

View Code

 

版权声明:本文为tkt2016原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tkt2016/p/8657584.html