如何在.NET中为外部方法提供自定义实现?
在 C# 中,extern
修饰符用于声明外部实现的方法。通常它与DllImport
属性一起使用来调用非托管代码中的某些函数。
我想知道是否有办法提供 extern
方法的自定义实现?
为了更好地理解该问题,请考虑以下用例。我有一组在非托管代码中实现的函数,我想在运行时(在加载程序集期间)提供指向这些函数的指针。
DllImport
属性的作用相同,但我想自己提供指针。
In C# the extern
modifier is used to declare a method that is implemented externally. Usually it is used with DllImport
attribute to call some function in unmanaged code.
I wonder if there is a way to provide custom implementation of extern
method?
To understand better the problem consider the following use case. I have a set of functions implemented in unmanaged code and I'd like to supply pointers to these function in run-time (during the loading of assembly).
The same thing DllImport
attribute does, but I'd like to provide pointers by myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可能的,尽管您需要 PInvoke 一些东西(
LoadLibrary
和GetProcAddress
- 请参阅下面的链接)...它被称为“后期绑定本机代码”...一些相关来源链接:
This is possible although you would need to PInvoke several things (
LoadLibrary
andGetProcAddress
- see links below)... it is called "late binding native code"...Some relevant links with source:
您需要使用 LoadLibrary 和GetProcAddress
You would need to use LoadLibrary and GetProcAddress