Get 'ldftn' function pointer in C #

in cil code, ldftn is used to get the address of the function pointer to invoke the delegate constructor (i.e. .ctor (object, native int)).
How to get the function pointer used to create a delegate in C #?

+5
source share
2 answers

Your question is formulated in such a way that it is difficult to understand what you are actually trying to do. I think maybe something like this:

MethodInfo mi = ...
var ptr = mi.MethodHandle.GetFunctionPointer();
// now call a delegate .ctor using that ptr
+5
source

If you are looking for what Reflection.Emit code should look like, then something like this:

il.Emit(OpCodes.Ldftn, yourMethodInfo);
il.Emit(OpCodes.Newobj, yourDelegateType.GetConstructors()[0]);

. "" . yourDelegateType - typeof(Func<string>) ..

+3

All Articles