I am using System.Reflection.Emit, and at some point I want to create a delegate from a MethodBuilder:
MethodBuilder fooBuilder = createFooMethodBuilder();
ILGenerator ilGenerator = ...
Type delegateType = typeof(DelegateType);
LocalBuilder delegateVar = ilGenerator.DeclareLocal(delegateType);
//Somehow create emit instructions to create delegate from fooBuilder
//Store delegate in delegateVar using
I could find out that to create delegates from static functions something like this is used:
ldnull
ldftn void class Test.MainClass::bar()
newobj instance void class Test.DelegateType::'.ctor'(object, native int)
But now I'm stuck. I need a way to ldftn the MethodBuilder and then I need a way to emit the instruction for the following line. And I have no idea how to get a constructor that accepts a native int.
Any suggestions?