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?
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:
Invoking this is 10 times faster than doing
methodInfo.Invoke(...)
(on amethodInfo
object that was created only once, of course).