从非托管 C++ 调用 C# 方法它传回完整的对象模型
我在 C# 中有类似以下内容的内容:
public class ClassA
{
int Id { get; set; }
ClassB[] ClassBItems { get; set; }
}
并且
public class ClassB
{
int SomeOtherId {get;set;}
}
我想将此对象模型传递到非托管 C++ 中。即从非托管 C++ 代码进行调用,例如“GetClassA() : ClassA”。
到目前为止,我已经成功地将单个对象或对象数组从托管 C# 传递到非托管 C++(使用 COM/CCW),但尚未传递包含 ClassB 的 ClassA。
我的问题是:
- 如何传回 ClassA 中的 ClassB 数组?
- 到目前为止,我只能从 C# 传回结构。我上面的例子是我实际上想传回的类。即对数据的引用。
澄清一下,非托管代码将调用托管代码。
I have something similar to the following in C#:
public class ClassA
{
int Id { get; set; }
ClassB[] ClassBItems { get; set; }
}
and
public class ClassB
{
int SomeOtherId {get;set;}
}
I want to pass this object model into unmanaged C++. I.e. Have a call from the unmanaged C++ code such as 'GetClassA() : ClassA'.
So far, I have managed to pass single objects or arrays of objects from managed C# to unmanaged C++ (using COM/CCW), but haven't been pass ClassA with ClassB inside it.
My questions are:
- How can I pass back ClassA which a ClassB array inside it?
- So far, I have only been able to pass back structs from C#. My examples above are classes which is what I'd actually like to pass back. I.e. a reference to the data.
To clarify, the unmanaged code will be calling the managed code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你实际上不能直接这样做。您无法从本机 C++ 调用 C# 方法,反之亦然。如果您传递某些内容,它始终只是一个简单的 C
struct
,没有方法。但有一些方法可以解决这个问题:
如果您不想这样做,您可以尝试通过将回调传递给非托管代码或类似的方法来实现。但我认为这会相对困难和混乱。
You can't really do that directly. You can't call C# methods from native C++ and vice versa. If you pass something, it's always just a simple C
struct
, with no methods.But there are some ways to work around that:
If you don't want to do either, you could try doing it by passing callbacks to the unmanaged code, or something like that. But I think it would be relatively hard and messy.