2 years ago

#75503

test-img

dvvrd

Signature of generic MethodRefs in .NET Profiler API

I am using the .NET profiler API to do IL rewriting. One of my objectives is to emit the type tokens of called method arguments. However, it gets tricky when it comes to generics. For instance, consider the following snippet.

class C<T>
{
    public void A(T x) {}
}
class Program
{
    public void F(C<int> l)
    {
        l.A(5);
    }
}

Method F(C<int>) gets compiled into the following IL:

.method public hidebysig (null) cil managed 
{
    // Code size 10 (0xa)
    .maxstack 8
    IL_0000:  nop 
    IL_0001:  ldarg.1 
    IL_0002:  ldc.i4.5 
    IL_0003:  callvirt instance void class C`1/*02000002(TypeDef)*/<int32>/*1b000001(TypeSpec)*/::A(!0)/*0a00000d(MemberRef)*/
    IL_0008:  nop 
    IL_0009:  ret 
}

Here, the callvirt instruction at offset IL_0003 is MemberRef 0x0a00000d. My goal now is to get the type token of the only argument of this method. To achieve that, I query the method signature using the IMetadataImport::GetMemberRefProps function, then parse it to get the type signatures of method arguments. However, I get the uninstantiated method signature: blob 0000001B hasthis System.Void (!0)

As for me, this is a bit weird, because MethodRef 0x0a00000d corresponds to the instantiation C<int>::A(int), and the type variable !0 should be instantiated with int.

Question: Is there any way to get the specialized generic method signature in .NET profiler API? In particular, how for the above example to obtain the type int of method parameter in result?

c#

.net

instrumentation

clr-profiling-api

0 Answers

Your Answer

Accepted video resources