我正在尝试使用C ++和C#在Visual Studio 2012中开发一个Native / from Managed项目。但是,我遇到了以下问题:
我在我的C#dll中定义了一个名为Shape
的对象,编码为:
ShapeFactory.cs
public class Shape
{
private Circle[] collection;
public Circle[] Collection { get { return this.collection; } set { this.collection = value; } }
private string color;
public string Color { get { return this.color; } set { this.color = value; } }
}
public class Circle
{
private Attributes[] attr;
public Attributes[] Attr { get { return this.attr; } set { this.attr = value; } }
private int radius;
public int Radius { get { return this.radius; } set { this.radius= value; } }
}
public class Attributes
{
private int height;
public int Height{ get { return this.height; } set { this.height = value; } }
private int width;
public int Width{ get { return this.width; } set { this.width = value; } }
}
ShapeFactoryCPP.cpp
#using "ShapeFactory.dll"
#include <msclr\gcroot.h>
#include "ShapeFactoryCPP.h"
void GetShape(ShapeManaged ^ managed)
{
ShapeManaged % managedRef = *managed;
int collectionCount = managedRef.Collection->Length;
if(collectionCount > 0)
{
for(int i = 0; i < collectionCount ; i++)
{
if(managedRef.Collection[i] != nullptr)
{
auto x = managedRef.Collection[i]->radius;
}
}
}
}
这被编译为ShapeFactory.dll
并在我的C ++中包含为#using ShapeFactory.dll
。我创建了一个Managed对象传递给C#函数,并将托管对象从C ++设置为Json.Net序列化对象,如上所示。但是,在尝试接听电话managedRef.Collection[0]->attr
或managedRef.Collection[0]->radius
时,我似乎无法在C ++中找到该集合的字段。 Visual Studio的IntelliSense会引发以下语法错误,但仍然可以编译并运行:
Error: class System:Object has no member "radius"
当我调试此内容并添加managedRef->Collection[i]->radius
作为监视变量时,它会显示正确的值,managedRef->Collection[i]
会显示集合并键入Circle。如何让IntelliSense了解集合的类型?我是否错过了IntelliSense的某个配置设置或定义了我的C#对象而没有提供C ++获取对象类型的必要信息?