Reputation: 418
which is the easiest way to return a list/vector/array of objects from c++/cli to c# in a typesafe way?
//C++/CLI project 1
public ref class MyClass
{
}
public ref class Factory
{
array<MyClass^> getObjects() {...}
}
//C# project 2
Factory f = new Factory();
System.Array a = f.getObjects(); // not typesafe! I'd like to get an array/list/vector of MyClass elements
I tried returning a
List<MyClass^>
from C++ and read an
IList<MyClass^>
from C#, it didn't compile...
thanks, Chris
Upvotes: 0
Views: 1691
Reputation: 2111
Array in C++/CLI is "managed pointer" (called reference), therefore it is written as array<Type>^
. You can initialize it via gcnew array<Type, dimension>(count)
which equals to C# new Type[count]
, when dimension is 1.
So in your case, it could look like this:
array<MyClass^>^ getObjects() {
return gcnew array<MyClass^,1>(number) ;
}
Upvotes: 1