开发者

get 'ldftn' function pointer in C#

in cil code, ldftn is used to get the function pointer address to call the delegate constructor开发者_JAVA百科(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.


ldftn available in C# 9 .NET 5 https://learn.microsoft.com/dotnet/core/dotnet-five

Function pointers: Language constructs that expose the following intermediate language (IL) opcodes: ldftn and calli.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜