8

I have the following C# code:

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

And here it's IL code:

.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
}

How can I to emit

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

insturction through the System.Reflection.Emit or better through the Mono.Cecil?

What !1 and !0 are stands for?

Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80

1 Answers1

10

The !n syntax is a reference to a generic argument.

In this example ...

!0 is a reference to the first generic argument of Func<double, double> (used as the type of the argument of the Invoke method)

!1 is a reference to the second generic generic argument of Func<double, double> (used as the return type of Invoke)

EDIT: Your method using 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: corrected the !n explanation after a hint of @kvb

ulrichb
  • 19,610
  • 8
  • 73
  • 87
  • 1
    Your `System.Reflection.Emit` code is correct, but your explanation of `!0` and `!1` is not - they refer to the generic type parameters of `System.Func<,>`, not to the stack. – kvb Oct 10 '12 at 18:06
  • Thank you. I didn't know that it's so simply :) Also, please, correct your answer with @kvb note. – Ivan Kochurkin Oct 10 '12 at 22:09
  • @kvb Are you really sure? If so, what does `callvirt instance !1` mean, what does `Invoke(!0)` mean. Any reference? Further, it seems that this is a special Reflector syntax, ILSpy and the LINQPad IL output doesn't have this `!n` syntax. – ulrichb Oct 10 '12 at 22:45
  • ILSpy and Microsoft IL Dasm certainly have !n syntax. – Ivan Kochurkin Oct 11 '12 at 06:23
  • I even say more: IL listing from question is from ILSpy. – Ivan Kochurkin Oct 11 '12 at 08:41
  • @ulrichb - Yes, I'm sure. The `!1` is the return type of the method and the `!0` is the type of the argument to `Invoke`. In a non-generic method you'd see something like `callvirt instance float64 class ...::Invoke(float64)`. Since `Func<,>` is generic, the generic type arguments take the place of the concrete parameter and return types. – kvb Oct 11 '12 at 15:49
  • @kvb Edited. Many thanks for your hints, to correct my answer. – ulrichb Oct 12 '12 at 11:26