简体   繁体   English

Reflection.Emit创建通用继承方法

[英]Reflection.Emit Create Generic Inherithed Methods

First, I want to say that this is for a college project, so I'm not looking for a solution, merely a help in understanding what I'm doing wrong so I can try to fix it. 首先,我想说这是一个大学项目,所以我不是在寻找解决方案,只是帮助理解我做错了什么,所以我可以尝试解决它。

I'm required to dynamicaly create a class that inherits another class. 我需要动态创建一个继承另一个类的类。 Each method is expected to call the base method, while adding extra code (in this case, to send MethodInfo to a static method, so I can count how many times the Method is invoked). 期望每个方法调用基本方法,同时添加额外的代码(在这种情况下,将MethodInfo发送到静态方法,因此我可以计算调用Method的次数)。

An example of what I want to do: 我想做的一个例子:

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

And dynamically create this: 并动态创建:

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

I'm able to do this, except for when there's generic methods...I've searched all over the internet, read MSND How To: Define Generic Method and such, but to no avail. 我能够做到这一点,除非有通用的方法...我在互联网上搜索过,阅读MSND如何:定义通用方法等,但无济于事。

My code to build the Method is as follow: (EDITED) 我构建方法的代码如下:(已编辑)

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);
}

When I attempt to create the type with TypeBuilder.CreateType(), it throws a TypeLoadException, saying that the signature of the body and declaration of the method do not match, which I've managed to find that the issue is about the Generic parameter. 当我尝试使用TypeBuilder.CreateType()创建类型时,它会抛出一个TypeLoadException,表示正文的签名和方法的声明不匹配,我已经设法发现该问题与Generic参数有关。 。

However, I cannot understand how to fix it. 但是,我无法理解如何解决它。

Any help would be appreciated. 任何帮助,将不胜感激。

Ok, I've been able to solve my problem...basically, I was getting confused with the meaning of MethodBuilder.DefineGenericParameter(..) 好吧,我已经能够解决我的问题...基本上,我对MethodBuilder.DefineGenericParameter(..)的含义感到困惑

Anyway, I added the following code 无论如何,我添加了以下代码

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);

So basically...I wasn't defining my generic parameters in the methodbuilder. 基本上......我没有在methodbuilder中定义我的泛型参数。

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

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