Tiago Magalhães
Tiago Magalhães

Reputation: 59

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

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.

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.

However, I cannot understand how to fix it.

Any help would be appreciated.

Upvotes: 4

Views: 425

Answers (1)

Tiago Magalh&#227;es
Tiago Magalh&#227;es

Reputation: 59

Ok, I've been able to solve my problem...basically, I was getting confused with the meaning of 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.

Upvotes: 1

Related Questions