MSIL实用指南-创建方法和定义参数

 本篇讲解实现创建方法、指定参数的名称、实现参数加out和ref修饰符、以及参数加默认值。

创建方法

创建方法用类TypeAttributes的

DefineMethod(string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)

方法,返回结果是MethodBuilder,就可以创普通方法。

例子

MethodBuilder m2 = typeBuilder.DefineMethod("M2",
MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
typeof(string), new Type[] { typeof(int), typeof(string), typeof(object) });

 

这个写法就和下面的C#程序一样

public abstract void M1();

定义一个抽象方法需要用MethodAttributes的Abstract|Virtual才可以。

 

定义参数

 

用MethodBuilder的DefineParameter(int position, ParameterAttributes attributes, string strParamName)

方法

 参数说明

position:该参数在参数列表中的位置。为参数编索引,第一个参数从数字 1 开始;数字 0 表示方法的返回值。

attributes: 参数的参数属性。

strParamName: 参数名。名称可以为 null 字符串。

返回结果:

返回一个 ParameterBuilder 对象,该对象表示此方法的参数或此方法的返回值。

1.指定参数的名称

在程序的方法调用中传入第三个参数传入参数名称就可以了。

例如

MethodBuilder m2 = typeBuilder.DefineMethod("M2",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { typeof(int), typeof(string), typeof(object) });

m2.DefineParameter(1, ParameterAttributes.None, "arg0");
m2.DefineParameter(2, ParameterAttributes.None, "param1");
m2.DefineParameter(3, ParameterAttributes.None, "param2");

 

这个方法的三个参数名称依次是arg0、param1、param2。

 

2.指定参数out

我们要实现如下方法,参数的修饰符是out

public abstract string mout(out int arg3 );

需要使用ParameterAttributes.Out就可以了。

具体实现

MethodBuilder m5 = typeBuilder.DefineMethod("mout",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { typeof(int), typeof(int) });

            ParameterBuilder m5p1 = m5.DefineParameter(1, ParameterAttributes.Out, "arg3");

 

3.实现参数默认值

要实现参数有默认值,比如下面这句

public abstract string mdefault(int arg1 =4 );

实现这个效果的程序是

MethodBuilder m3 = typeBuilder.DefineMethod("mdefault",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { typeof(int) });

            ParameterBuilder m3p1 = m3.DefineParameter(1, ParameterAttributes.HasDefault | ParameterAttributes.Optional, "arg1");
            m3p1.SetConstant(4);

实现定义参数属性为ParameterAttributes.HasDefault | ParameterAttributes.Optional,并得到一个

ParameterBuilder实例,再设置这个实例的默认值。

 

4.实现指定参数ref

我们要实现如下方法,参数的修饰符是out

public abstract string mref(ref dobule arg2);

 

首先

MethodBuilder m4 = typeBuilder.DefineMethod("mref",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { Type.GetType("System.Double&") });

参数的类型不是一般的typeof(double),而要用特殊的Type.GetType("System.Double&")

 

其次

ParameterBuilder m4p1 = m4.DefineParameter(1, ParameterAttributes.Out, "arg2");

 

要设置参数的属性为ParameterAttributes.Out

 

完整程序如下

 

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace LX1_ILDemo
{
    class Demo07_Method
    {
        static string binaryName = "Demo07_Method.dll";
        static string namespaceName = "LX1_ILDemo";
        static string typeName = "EmitMethod";

        static AssemblyBuilder assemblyBuilder;
        static ModuleBuilder moduleBuilder;
        static TypeBuilder typeBuilder;

        public static void Generate()
        {
            InitAssembly();

            typeBuilder = moduleBuilder.DefineType(namespaceName + "." + typeName,
                TypeAttributes.Public | TypeAttributes.Abstract);

            /* 生成 public static void Main() */
            GenerateMethods();

            SaveAssembly();
            Console.WriteLine("生成成功");
        }

        static void GenerateMethods()
        {
            /* public abstract void M1(); */
            MethodBuilder m1 = typeBuilder.DefineMethod("M1",
                MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
                typeof(void), new Type[] { });

            /* public abstract string M2(int arg0,string param1); */
            MethodBuilder m2 = typeBuilder.DefineMethod("M2",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { typeof(int), typeof(string), typeof(object) });

            m2.DefineParameter(1, ParameterAttributes.None, "arg0");
            m2.DefineParameter(2, ParameterAttributes.None, "param1");
            m2.DefineParameter(3, ParameterAttributes.None, "param2");

            /* public abstract string mdefault(int arg1 =4 ); */
            MethodBuilder m3 = typeBuilder.DefineMethod("mdefault",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { typeof(int) });

            ParameterBuilder m3p1 = m3.DefineParameter(1, ParameterAttributes.HasDefault | ParameterAttributes.Optional, "arg1");
            m3p1.SetConstant(4);

            /* public abstract string mref(ref dobule arg2); */
            MethodBuilder m4 = typeBuilder.DefineMethod("mref",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { Type.GetType("System.Double&") });

            ParameterBuilder m4p1 = m4.DefineParameter(1, ParameterAttributes.Out, "arg2");


            /* public abstract string mout(out int arg3 ); */
            MethodBuilder m5 = typeBuilder.DefineMethod("mout",
               MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
               typeof(string), new Type[] { typeof(int), typeof(int) });

            ParameterBuilder m5p1 = m5.DefineParameter(1, ParameterAttributes.Out, "arg3");
        }


        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

 

转载于:https://www.cnblogs.com/tkt2016/p/8617438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值