in cil code, ldftn is used to get the function pointer address to call the delegate constructor(i.e. .ctor(object, native int)).
How to get the function pointer used to construct delegate in C#?
Your question is phrased in a way that makes it hard to understand what you're actually trying to do. I think that perhaps what you want is something like this:
MethodInfo mi = ...
var ptr = mi.MethodHandle.GetFunctionPointer();
// now call a delegate .ctor using that ptr
If you're looking for how the Reflection.Emit code should look, then something like this:
il.Emit(OpCodes.Ldftn, yourMethodInfo);
il.Emit(OpCodes.Newobj, yourDelegateType.GetConstructors()[0]);
The first line loads the function pointer onto the stack. The second line "passes" it to the constructor of the delegate. yourDelegateType
should be something like typeof(Func<string>)
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With