在C#中调用动态方法时如何传递Struct参数?

时间:2018-12-20 03:32:53

标签: c# emit dynamicmethod

我在动态方法中无法通过struct参数。 这是我的代码:

public class Program
{
    static void Main(string[] args)
    {
        var DynamicAssembly = new AssemblyName();
        DynamicAssembly.Name = "DynamicTypes";
        AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(DynamicAssembly, AssemblyBuilderAccess.Run);
        ModuleBuilder mb = ab.DefineDynamicModule(DynamicAssembly.Name);
        TypeBuilder tb = mb.DefineType("Handler", TypeAttributes.Class | TypeAttributes.Public);
        MethodBuilder handler = tb.DefineMethod($"DynamicHandler",
            MethodAttributes.Public | MethodAttributes.Static,
           typeof(void),
           new Type[] { typeof(MyClass), typeof(MyStruct) });
        var ProcessMethod = typeof(Program).GetMethod(nameof(Process));
        ILGenerator il = handler.GetILGenerator();
        il.Emit(OpCodes.Nop);
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldarg_1);
        il.EmitCall(OpCodes.Call, ProcessMethod, null);
        il.Emit(OpCodes.Nop);
        il.Emit(OpCodes.Ret);
        var DynamicType = tb.CreateType();
        MethodInfo methodInfo = DynamicType.GetMethod("DynamicHandler");
        int i = 100;
        while (true)
        {
            i++;
            MyClass a = new MyClass()
            {
                a = i,
            };
            MyStruct b = new MyStruct()
            {
                b = i,
            };
            methodInfo.Invoke(null, new object[] { a, b });
            Thread.Sleep(3000);
        }
    }
    public static void Process(object arg1, object arg2)
    {
        Console.WriteLine($"arg1:{arg1} arg2:{arg2}");
    }
}
public struct MyStruct
{
    public int a;
    public int b;
}
public class MyClass
{
    public int a;
    public int b;
}

当我将代码运行到Process方法参数arg2时,发现'FatalExecutionEngineError'无法读取内存。

但是如果我将第二个参数更改为MyStruct

    public static void Process(object arg1,MyStruct arg2)
    {
        Console.WriteLine($"arg1:{arg1} arg2:{arg2}");
    }

它正常工作,但这不是我想要的。

我认为我的IL代码不正确,但是我不知道它在哪里。

1 个答案:

答案 0 :(得分:1)

您必须在OpCodes.Box上调用MyStruct才能将其作为object传递为方法的参数。

您也不需要致电OpCodes.Nop。 所以基本上您的代码应该是→

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Box, typeof(MyStruct));
//Rest is the same