繁体   English   中英

Reflection.Emit创建通用继承方法

[英]Reflection.Emit Create Generic Inherithed Methods

首先,我想说这是一个大学项目,所以我不是在寻找解决方案,只是帮助理解我做错了什么,所以我可以尝试解决它。

我需要动态创建一个继承另一个类的类。 期望每个方法调用基本方法,同时添加额外的代码(在这种情况下,将MethodInfo发送到静态方法,因此我可以计算调用Method的次数)。

我想做的一个例子:

public class A
{
   public virtual void M1(B arg0, int arg1)
   { //do stuff
   }
}

并动态创建:

public class InstrA : A
{
    public override void M1(B arg0, int arg1)
    {
       base.M1(arg0,arg1)
       ProfilerCounter.LogMethod(typeof(InstrA).GetMethod("M1"));
    }
}

我能够做到这一点,除非有通用的方法...我在互联网上搜索过,阅读MSND如何:定义通用方法等,但无济于事。

我构建方法的代码如下:(已编辑)

public static void BuildMethod(MethodInfo method, TypeBuilder dest)
{           
    Type[] param_types = GetParameterTypes(method.GetParameters());
    MethodBuilder mb = dest.DefineMethod(
       method.Name,
       method.Attributes);

    Type toReturn = method.ReturnType;

    mb.SetReturnType(toReturn);
    mb.SetParameters(param_types);        

    //from here I create the IL code, so that it calls base and then adds the methodinfo to a counter
    //so that everytime the method is run, it will register
    var getMethodMethod = typeof(MethodBase).GetMethod(
            "GetMethodFromHandle",
            new[] { typeof(RuntimeMethodHandle) });          

        ILGenerator il_method = mb.GetILGenerator();
        il_method.Emit(OpCodes.Ldarg_0);
        for (int i = 0; i < param_types.Length; i++)
        {
            il_method.Emit(OpCodes.Ldarg, i + 1);
        }
        il_method.Emit(OpCodes.Call, method);
        il_method.Emit(OpCodes.Ldtoken, method);
        il_method.Emit(OpCodes.Call, getMethodMethod);
        il_method.Emit(OpCodes.Castclass, typeof(MethodInfo));           
        il_method.Emit(OpCodes.Call, typeof(ProfilerCounter).GetMethod("LogMethod"));
        il_method.Emit(OpCodes.Ret);

        dest.DefineMethodOverride(mb, method);
}

当我尝试使用TypeBuilder.CreateType()创建类型时,它会抛出一个TypeLoadException,表示正文的签名和方法的声明不匹配,我已经设法发现该问题与Generic参数有关。 。

但是,我无法理解如何解决它。

任何帮助,将不胜感激。

好吧,我已经能够解决我的问题...基本上,我对MethodBuilder.DefineGenericParameter(..)的含义感到困惑

无论如何,我添加了以下代码

MethodBuilder mb = dest.DefineMethod(
   method.Name,
   method.Attributes);

if (method.IsGenericMethod)
{
    Type[] genericTypes = method.GetGenericArguments();
    foreach (Type t in genericTypes)
    {
        mb.DefineGenericParameters(t.Name);
    }
}

mb.SetReturnType(method.ReturnType);
mb.SetParameters(param_types);

基本上......我没有在methodbuilder中定义我的泛型参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM