1

DynamicMethods 允许您为您创建的委托指定目标实例。但是,当您使用结构类型时,这似乎不起作用。它失败并出现异常,告诉我它无法绑定到此方法。错误是因为我的 IL 没有拆箱目标实例吗?

如果我在这里将 A 更改为一个类,它可以正常工作。我究竟做错了什么?(另外请不要建议调用Delegate.CreateDelegate绑定到带有目标实例的GetType方法)

这是一个示例复制:

struct A { }
... //then some where in code::
Func<Type> f = CodeGen.CreateDelegate<Func<Type>>(il=>
    il.ldarga_s(0)
    .constrained(typeof(A))
    .callvirt(typeof(object).GetMethod("GetType"))
    .ret(),
    name:"Constrained",
    target:new A()
);

注意:我正在Emitted为 IL 的流利界面使用该库。此外,这里是 CodeGen 方法的代码。

public static class CodeGen
{
    public static TDelegate CreateDelegate<TDelegate>(Action<ILGenerator> genFunc, string name = "", object target = null, bool restrictedSkipVisibility = false)
        where TDelegate:class
    {
        ArgumentValidator.AssertGenericIsDelegateType(() => typeof(TDelegate));
        ArgumentValidator.AssertIsNotNull(() => genFunc);

        var invokeMethod = typeof(TDelegate).GetMethod("Invoke");
        var @params = invokeMethod.GetParameters();
        var paramTypes = new Type[@params.Length + 1];
        paramTypes[0] = target == null ? typeof(object) : target.GetType();
        @params.ConvertAll(p => p.ParameterType)
            .CopyTo(paramTypes, 1);
        var method = new DynamicMethod(name ?? string.Empty, invokeMethod.ReturnType, paramTypes, restrictedSkipVisibility);
        genFunc(method.GetILGenerator());

        return method.CreateDelegate<TDelegate>(target);
    }
}
4

1 回答 1

0

请参阅http://msdn.microsoft.com/en-us/library/74x8f551.aspx上的重要说明,这也适用于此处:

如果方法是静态的(在 Visual Basic 中为 Shared)并且其第一个参数是 Object 或 ValueType 类型,则 firstArgument 可以是值类型。在这种情况下 firstArgument 被自动装箱。不会像在 C# 或 Visual Basic 函数调用中那样对任何其他参数进行自动装箱。

这意味着您的动态方法的第一个参数需要是 type object,并且在进行约束调用之前,您需要先执行一个 ldarg_0 ,然后是一个 unbox 。

于 2011-10-27T20:55:33.163 回答