得到“ldftn” C# 中的函数指针

发布于 2024-09-27 18:33:36 字数 93 浏览 0 评论 0原文

在cil代码中,ldftn用于获取函数指针地址来调用委托构造函数(即.ctor(object, native int))。
在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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼忆七猫命九 2024-10-04 18:33:36

你的问题的措辞方式让人很难理解你实际上想要做什么。我想也许你想要的是这样的:

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

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
拧巴小姐 2024-10-04 18:33:36

如果您正在寻找 Reflection.Emit 代码的外观,那么如下所示:

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

第一行将函数指针加载到堆栈上。第二行将其“传递”给委托的构造函数。 yourDelegateType 应该类似于 typeof(Func) 等。

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.

屋檐 2024-10-04 18:33:36

ldftn 在 C# 9 .NET 5 中可用
https://learn.microsoft.com/dotnet/core/dotnet- Five

函数指针:公开以下中间语言 (IL) 操作码的语言构造:ldftn 和 calli。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文