"操作可以使运行时不稳定"和DynamicMethod使用值类型

Jim*_*ard 6 c# cil

我试图推广以下IL(来自Reflector):

.method private hidebysig instance void SetValue(valuetype Test.TestFixture/ValueSource& thing, string 'value') cil managed
{
    .maxstack 8
    L_0000: nop 
    L_0001: ldarg.1 
    L_0002: ldarg.2 
    L_0003: call instance void Test.TestFixture/ValueSource::set_Value(string)
    L_0008: nop 
    L_0009: ret 
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用DynamicMethod重现此IL时:

        [Test]
    public void Test_with_DynamicMethod()
    {
        var sourceType = typeof(ValueSource);
        PropertyInfo property = sourceType.GetProperty("Value");

        var setter = property.GetSetMethod(true);
        var method = new DynamicMethod("Set" + property.Name, null, new[] { sourceType.MakeByRefType(), typeof(string) }, true);
        var gen = method.GetILGenerator();

        gen.Emit(OpCodes.Ldarg_1); // Load input to stack
        gen.Emit(OpCodes.Ldarg_2); // Load value to stack
        gen.Emit(OpCodes.Call, setter); // Call the setter method
        gen.Emit(OpCodes.Ret);

        var result = (SetValueDelegate)method.CreateDelegate(typeof(SetValueDelegate));

        var source = new ValueSource();

        result(ref source, "hello");

        source.Value.ShouldEqual("hello");
    }

    public delegate void SetValueDelegate(ref ValueSource source, string value);
Run Code Online (Sandbox Code Playgroud)

我得到一个例外,"操作可能会破坏运行时的稳定性".IL似乎与我相同,任何想法?ValueSource是一个值类型,这就是我在这里做一个ref参数的原因.

编辑

这是ValueSource类型:

public struct ValueSource
{
   public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 4

将 args 更改为 0/1(而不是 1/2):

    gen.Emit(OpCodes.Ldarg_0); // Load input to stack
    gen.Emit(OpCodes.Ldarg_1); // Load value to stack
Run Code Online (Sandbox Code Playgroud)

因为动态方法似乎被创建为静态方法,而不是实例(您的原始方法是实例) - 因此参数减少了 1。

(对于最初的错误答案感到抱歉 - 您可以将另一部分代码保留为true