托管C++;到C#
首先,我想提及这个问题: 这似乎是我正在寻找的,但在尝试实现这一点时,我得到了一些错误 首先,这是我的代码: C++MyCppWrapper.h托管C++;到C#,c#,list,managed-c++,C#,List,Managed C++,首先,我想提及这个问题: 这似乎是我正在寻找的,但在尝试实现这一点时,我得到了一些错误 首先,这是我的代码: C++MyCppWrapper.h namespace CppWrapping { #pragma pack(1) public struct MyPoint { public: float X; float Y; float Z; unsigned char R; unsi
namespace CppWrapping
{
#pragma pack(1)
public struct MyPoint
{
public:
float X;
float Y;
float Z;
unsigned char R;
unsigned char G;
unsigned char B;
unsigned char A;
}MyPoint_t;
#pragma pack()
public ref class MyCppWrapper
{
public:
MyCpplWrapper(void);
List<MyPoint>^ getData();
};
};
我还在代码List data=\u wrapper.getData()下得到一个红色标记代码>当我将光标悬停在其上时:
Cannot convert source type 'System.Collections.Generic.List<CppWrapping.MyPoint>' to target type 'System.Collections.Generic.List<ProjectA.MyLinker.MyPoint_t>'
这声明了一个非托管结构,您的C#代码无法访问它,因为它是作为不透明值类型导出的,没有任何成员。你必须申报为
public value struct MyPoint {}
接下来要做的是从C代码中删除MyPoint声明。类型标识包括类型来自的程序集,因此MyPoint与MyPoint不兼容。您只需使用C++/CLI程序集中的MyPoint类型:
_wrapper = new MyCppWrapper();
List<MyPoint> data = _wrapper.getData();
“'CppWrapping.MyPoint'由于其保护级别而不可访问”只意味着需要是公共的方法/属性/类/结构不可访问。Easy to fix.CppWrapping.MyPoint已经是公共的(请参阅:代码上部)。
Cannot convert source type 'System.Collections.Generic.List<CppWrapping.MyPoint>' to target type 'System.Collections.Generic.List<ProjectA.MyLinker.MyPoint_t>'
Cannot implicitly convert type 'System.Collections.Generic.List<CppWrapping.MyPoint>' to 'System.Collections.Generic.List<ProjectA.MyLinker.MyPoint_t>'
public struct MyPoint {}
public value struct MyPoint {}
_wrapper = new MyCppWrapper();
List<MyPoint> data = _wrapper.getData();
var data = _wrapper.getData();