如何使用PInvoke导入功能模板? [英] How would I import a function template using PInvoke?
问题描述
在我的C#代码中,我需要从我编写的C ++ Dll中调用一个函数,该函数是通用的. 因此,我应该像这样导入它吗?
In my C# code, I need to call a function from a C++ Dll that I wrote.The function is generic. So , should I just import it like this:
[DllImport("myDll.dll")]
private static extern TypeName functionName<TypeName>( int arg1, int arg2 );
这是正确的语法吗? 谢谢.
Is this correct syntax? Thanks.
推荐答案
这无法正常工作,因为没有主流的C ++编译器可以使模板可导出.此外,C ++编译器通过类型擦除将模板实例化,类似于Java泛型的工作方式.换句话说,具体的可调用函数必须由C ++编译器嵌入到DLL中.它们不再通用.
This cannot work, there is no main-stream C++ compiler that makes templates exportable. Furthermore, templates are instantiated by the C++ compiler through type erasure, similar to the way Java generics works. In other words, the concrete callable functions have to be embedded in the DLL by the C++ compiler. They are no longer generic.
作为替代,您可以使用C ++/CLI语言编写ref类.这样就产生了一个真正的.NET泛型类,可以由支持泛型的任何.NET语言使用.
As an alternative, you can write a ref class in the C++/CLI language. That produces a true .NET generic class, usable by any .NET language that supports generics.
这篇关于如何使用PInvoke导入功能模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!