【发布时间】:2015-08-13 05:03:47
【问题描述】:
我有一个托管的 C++ 函数,它返回一个类数组:
托管 C++ 代码:
public ref class Sample
{
public:
double Open;
double Close;
};
public ref class ManagedTest
{
array<Sample^>^ ManagedTest::TestFunction()
{
//body
}
};
TestFunction() 在 C# 应用程序中被调用。我在 C# 中创建了相同的 Sample 类并尝试获取返回值。但是我得到关于转换的编译错误。
这是我的 C# 代码:
[StructLayout(LayoutKind.Sequential, Size = 100), Serializable]
public class Sample
{
public double Open;
public double Close;
}
//No. 100 I can manage, just written a random number
Sample[] randomValues = new Sample[100]; //Array of random numbers
GCHandle handle = GCHandle.Alloc(randomValues, GCHandleType.Pinned);
var randPtr = handle.AddrOfPinnedObject();
var test = new ManagedTest();
randPtr = test.TestFunction();
如何将此托管 C++ 类数组转换为 C# 类数组?
【问题讨论】:
-
你能提供你的c#类吗
-
如果您能够编辑签名,请参阅此类似问题。我认为答案提供了一个更好的方法来解决这个问题:stackoverflow.com/questions/15806393/…
标签: c# managed-c++