简体   繁体   English

无法从C#引用托管C ++类

[英]Managed C++ class cannot be referenced from C#

The problem I am currently encountering is really weird. 我目前遇到的问题确实很奇怪。 I try to use a Managed C++ class from a C# project but the compiler cannot find the type. 我尝试使用C#项目中的Managed C ++类,但是编译器找不到该类型。

Take a look at this screenshot: 看一下这个屏幕截图: 问题截图

The Managed C++ project (NGervill.Gervill.Native) is referenced and according to the Object Browser it contains all required namespaces and types. 引用了托管C ++项目(NGervill.Gervill.Native),根据对象浏览器,它包含所有必需的名称空间和类型。 In my source code I've added the using and used the class but still I get a compile error. 在我的源代码中,我已经添加了using和using类,但是仍然出现编译错误。

Now the strangest part: The other types within the Managed C++ project can be referenced. 现在最奇怪的部分:可以引用Managed C ++项目中的其他类型。 In another C# class I access the methods of the PortMixerProviderNative class. 在另一个C#类中,我访问PortMixerProviderNative类的方法。 That means target platform and .net framework version are correct (.net 4.5 - x86 build). 这意味着目标平台和.net框架版本正确(.net 4.5-x86构建)。

Is Visual Studio somehow caching an old version of the Managed C++ DLL or what else can cause such a problem? Visual Studio是否以某种方式缓存了托管C ++ DLL的旧版本,或者还有哪些其他原因可以导致这种问题?

PortMixerNative is a native C++ class, not a managed class. PortMixerNative是本机C ++类,而不是托管类。 Native classes can be exposed in the assembly metadata in some cases, usually because they are the type of a private field in a managed class wrapper, but they are not usable in any way from a C# program. 在某些情况下,本机类可以在程序集元数据中公开,通常是因为它们是托管类包装器中私有字段的类型,但是它们不能以任何方式在C#程序中使用。 Only public ref class declarations in the C++/CLI project are usable. 仅C ++ / CLI项目中的public ref class声明可用。

It isn't clear what wrapper class you are supposed to use. 目前尚不清楚您应该使用哪种包装器类。 Not PortMixerNative. 不是PortMixerNative。 Check the vendor's manual and/or code samples or contact them if you need more help. 检查供应商的手册和/或代码示例,或者如果需要更多帮助,请与他们联系。

I finally found the solution of this problem: For some reason the cpp file implementing the PortMixerNative class was not included in the project. 我终于找到了解决该问题的方法:由于某种原因,实现PortMixerNative类的cpp文件未包含在项目中。 After adding the PortMixerNative.cpp to the project again I finally could see that there were syntax errors in this file. 将PortMixerNative.cpp再次添加到项目后,我终于可以看到该文件中存在语法错误。 After fixing the errors and recompiling the .Native project I could successfully reference the class. 修复错误并重新编译.Native项目后,我可以成功引用该类。

The strange thing is that the .Native project successfully compiled even with methods without implementations. 奇怪的是,即使使用没有实现的方法,.Native项目也可以成功编译。 If anybody encounters this problem too, check if all methods of the class you try to use have an implementation. 如果有人也遇到此问题,请检查您尝试使用的类的所有方法是否都有实现。

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

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