Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get 'ldftn' function pointer in C#

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#?

like image 526
Kii Avatar asked Oct 12 '10 14:10

Kii


2 Answers

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
like image 134
kvb Avatar answered Sep 19 '22 16:09

kvb


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.

like image 38
Kirk Woll Avatar answered Sep 18 '22 16:09

Kirk Woll