简体   繁体   English

如何为.NET中的extern方法提供自定义实现?

[英]How to provide custom implementation for extern methods in .NET?

In C# the extern modifier is used to declare a method that is implemented externally. 在C#中, extern修饰符用于声明在外部实现的方法。 Usually it is used with DllImport attribute to call some function in unmanaged code. 通常,它与DllImport属性一起使用以在非托管代码中调用某些函数。

I wonder if there is a way to provide custom implementation of extern method? 我想知道是否有一种方法可以提供自定义实现的extern方法吗?

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. DllImport属性也做同样的事情,但是我想自己提供指针。

This is possible although you would need to PInvoke several things ( LoadLibrary and GetProcAddress - see links below)... it is called "late binding native code"... 这是可能的,尽管您需要PInvoke几件事( LoadLibraryGetProcAddress请参阅下面的链接)...它被称为“后期绑定本机代码” ...

Some relevant links with source: 与来源相关的一些链接:

您将需要使用LoadLibraryGetProcAddress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM