IL code to call method with out parameter

608 views Asked by At

I want to IL generate a dynamic method

delegate ArraySegment<byte> X(MyClass mc);

that calls a method of the signature on mc and returns its out parameter.

MethodInfo methInf = aClass.GetMethod("Y",
    BindingFlags.Public | BindingFlags.Instance, 
    null, new[] { typeof(ArraySegment<byte>).MakeByRefType() }, null);

but I don't know how to handle the out parameter. Here's the code I have so far.

DynamicMethod dm = new DynamicMethod("X", typeof(ArraySegment<byte>),
                                     new[] { typeof(MyClass) });
ILGenerator il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Callvirt, methInf, null);

What's needed to make the out param work?

1

There are 1 answers

0
Evgeniy Berezovsky On BEST ANSWER

Thanks @MarcGravell (also for your deleted answer, which was of great help, as it spells out what you hint at in your comment to my question) and ILSpy, which helped me by compiling c# code to CIL, so I could just peek at that.

So here's the final, working code:

LocalBuilder local = il.DeclareLocal(typeof(ArraySegment<byte>));
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloca, local);
il.EmitCall(OpCodes.Callvirt, methInf, null);
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldloc, local.LocalIndex);
il.Emit(OpCodes.Ret);

Invoking this is 10 times faster than doing methodInfo.Invoke(...) (on a methodInfo object that was created only once, of course).