Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# emit call to generic method

Having the class

class C
{
    public T Get<T>()
    {
        return default;
    }

    public void M()
    {
        int i = this.Get<Int32>();
    }
}

I want to generate the body of M at runtime using Reflection.Emit and ILGenerator so that it exactly resembles what is shown above.


What I tried is

ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Call, typeof(C).GetMethod(nameof(C.Get), BindingFlags.Instance));

which yields

ldarg.0
call instance !!0 C::Get<M0>(string)
//                       ^^
ret

but I need to get

ldarg.0
call instance !!0 C::Get<int32>(string)
//                       ^^^^^
ret

(note the different type parameters in the call to C.Get<T>)


When emitting a call to a generic cfunction, how do I pass the type of the generic parameter (i.e. get rid of the M0 and have it say int32 instead)?

like image 979
Thomas Flinkow Avatar asked Jan 01 '23 14:01

Thomas Flinkow


1 Answers

You need to substitute the type arguments with MakeGenericMethod:

ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit
( 
  OpCodes.Call, 
  typeof(C)
  .GetMethod(nameof(C.Get), BindingFlags.Instance)
  .MakeGenericMethod(typeof(int))
);
like image 200
Luaan Avatar answered Jan 13 '23 11:01

Luaan