在 Calli 指令中使用 Reflection.Emit 生成 modopt

Generating modopt using Reflection.Emit in Calli instruction

我正在尝试使用 Reflection.Emit 为以下代码的 Call 方法生成代码:

public unsafe class Program
{
    public struct ATest
    {
        public int Test;
    }
    
    public struct Test
    {
        public Vtable* vtbl;
        
        public struct Vtable
        {
            public delegate * unmanaged[Stdcall]<Test*, ATest> ptr;
        }
    }
    
    public static ATest Call(Test* instance)
    {
        return ((delegate * unmanaged[Stdcall, MemberFunction]<Test*, ATest>)instance->vtbl->ptr)(instance);
    }
    
}

我已经使用 sharplab.io 反编译了该方法并得到了以下 msil:

// Methods
.method public hidebysig static 
    valuetype Program/ATest Call (
        valuetype Program/Test* 'instance'
    ) cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 25 (0x19)
    .maxstack 2
    .locals init (
        [0] method unmanaged valuetype Program/ATest modopt([System.Private.CoreLib]System.Runtime.CompilerServices.CallConvMemberFunction) modopt([System.Private.CoreLib]System.Runtime.CompilerServices.CallConvStdcall) *(valuetype Program/Test*),
        [1] valuetype Program/ATest
    )

    IL_0000: nop
    IL_0001: ldarg.0
    IL_0002: ldfld valuetype Program/Test/Vtable* Program/Test::vtbl
    IL_0007: ldfld method unmanaged stdcall valuetype Program/ATest *(valuetype Program/Test*) Program/Test/Vtable::ptr
    IL_000c: stloc.0
    IL_000d: ldarg.0
    IL_000e: ldloc.0
    IL_000f: calli unmanaged valuetype Program/ATest modopt([System.Private.CoreLib]System.Runtime.CompilerServices.CallConvMemberFunction) modopt([System.Private.CoreLib]System.Runtime.CompilerServices.CallConvStdcall)(valuetype Program/Test*)
    IL_0014: stloc.1
    IL_0015: br.s IL_0017

    IL_0017: ldloc.1
    IL_0018: ret
} // end of method Program::Call

我尝试使用 reflection.emit 生成 MSIL 指令,但我不知道如何发出行 IL_000f:

var il = dynamicMethod.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, vtableField);
il.Emit(OpCodes.Ldfld, methodField);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc_0);

il.EmitCalli(???);

问题是,MSIL 中的这一行同时具有 modopt(MemberFunction)mopopt(Stdcall),而且我在 il.EmitCalli 方法的任何地方都没有看到它。

有人可以帮忙吗?

如问题https://github.com/dotnet/runtime/issues/11354中所述,目前无法在反射堆栈中使用函数指针(目前typeof(delegate* ...)总是returnsIntPtr),所以有在这种情况下,无法使 Reflection.Emit 可靠地工作。