如何有效地将对象向量返回到托管代码?
我有一个 ref 类,其中包含指向非托管类的指针。该类具有一些基本类型以及另一个类的对象向量。我想知道从托管代码获取和设置向量的最佳方法。 unmangedb 对象之间的 memcpy 是否有效或设置 unmangedb 的每个成员变量?
例如(假设该类已完成。我正在编写与问题相关的内容)
假设我们已经有一个名为 B 的 struct UnmanagementB 的托管包装。
struct UnmanagedA
{
int a;
vector<UnmanagedB> list;
};
public ref class A : public System::IDisposable
{
public:
// properties
property System::UInt32 a
{
System::UInt32 get();
void set(System::UInt32 value);
}
property array<B^>^ list
{
System::array<B^>^ get(); // what is the best way to set and get the vector
void set(array<B^>^ value);
}
private:
UnmanagedA* obj1;
};
I have a ref class that contains a pointer to an unmanaged class. the class has some basic types and also a vector of objects of another class. I would like to know the best way to get and set the vector from managed code. Will a memcpy between unmangedb objects be efficient or setting each member variable of unmanagedb?
for ex (assume the class is complete. I am writing what is relevant to the question)
Assume we already have a managed wrapped for struct UnmanagedB called B.
struct UnmanagedA
{
int a;
vector<UnmanagedB> list;
};
public ref class A : public System::IDisposable
{
public:
// properties
property System::UInt32 a
{
System::UInt32 get();
void set(System::UInt32 value);
}
property array<B^>^ list
{
System::array<B^>^ get(); // what is the best way to set and get the vector
void set(array<B^>^ value);
}
private:
UnmanagedA* obj1;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这显然不可能完全实现,因为
UnmanagementA
包含UnmanagementB
值的向量,而 A 公开array
类型的属性。代码>.如果这是有意的而不是拼写错误,则您需要将B^
的内容编组到UnmanagementB
的实例中。否则,让UnmanagedA
保存一个std::vector
B* >
并注意适当的生命周期管理。This obviously won't be cleanly possible, since
UnmanagedA
contains a vector ofUnmanagedB
values, while A exposes an property of typearray<B^>
. If this is intended and not a typo, you will need to marshall the content ofB^
into instances ofUnmanagedB
. Otherwise, letUnmanagedA
hold astd::vector< B* >
and take care of proper lifetime management.