【问题标题】:Emitting delegate function call发出委托函数调用
【发布时间】:2012-09-30 07:04:00
【问题描述】:

我有以下 C# 代码:

public static double f2(Func<double, double> f, double x)
{
    return f(x);
}   

这里是 IL 代码:

.method public hidebysig static 
    float64 f2 (
        class [mscorlib]System.Func`2<float64, float64> f,
        float64 x
    ) cil managed 
{
    // Method begins at RVA 0x20bd
    // Code size 8 (0x8)
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: ldarg.1
    IL_0002: callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0)
    IL_0007: ret
}

我怎样才能发射

callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0)

通过 System.Reflection.Emit 或通过 Mono.Cecil 更好地指导?

!1 和 !0 代表什么?

【问题讨论】:

    标签: c# .net reflection.emit il mono.cecil


    【解决方案1】:

    !n 语法是对泛型参数的引用。

    在这个例子中...

    !0 是对Func&lt;double, double&gt; 的第一个泛型参数的引用(用作Invoke 方法的参数类型)

    !1 是对Func&lt;double, double&gt; 的第二个泛型泛型参数的引用(用作Invoke 的返回类型)

    编辑:你使用System.Reflection.Emit的方法...

    var dynamicMethod = new DynamicMethod(
        "f2Dynamic", 
        typeof(double), 
        new Type[] { typeof(Func<double, double>), typeof(double) });
    
    var il = dynamicMethod.GetILGenerator();
    
    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Ldarg_1);
    il.Emit(OpCodes.Callvirt, typeof(Func<double, double>).GetMethod("Invoke"));
    il.Emit(OpCodes.Ret);
    
    var f2Dynamic = 
        (Func<Func<double, double>, double, double>)dynamicMethod.CreateDelegate(
            typeof(Func<Func<double, double>, double, double>));
    
    Console.WriteLine(f2(x => x * x, 10.0));        // prints 100
    Console.WriteLine(f2Dynamic(x => x * x, 10.0)); // prints 100
    

    EDIT2:在提示@kvb 后更正了!n 的解释

    【讨论】:

    • 您的System.Reflection.Emit 代码是正确的,但您对!0!1 的解释不正确-它们指的是System.Func&lt;,&gt; 的泛型类型参数,而不是堆栈。
    • 谢谢。我不知道它是如此简单:) 另外,请用@kvb note 更正您的答案。
    • @kvb 你确定吗?如果是这样,callvirt instance !1 是什么意思,Invoke(!0) 是什么意思。有什么参考吗?此外,这似乎是一种特殊的 Reflector 语法,ILSpy 和 LINQPad IL 输出没有这种 !n 语法。
    • ILSpy 和 Microsoft IL Dasm 肯定有 !n 语法。
    • 我什至还要说:问题中的 IL 列表来自 ILSpy。
    猜你喜欢
    • 2019-09-20
    • 2021-05-31
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多