将本机类型的 C++CLI 包装器传递给另一个 C++/CLI 程序集

发布于 2024-11-08 21:41:36 字数 1174 浏览 0 评论 0 原文

假设我有以下 NativeClassInstance 的简单包装。

public ref class Wrapper
{
private:
    NativeClass *_wrapped;
public:
    Renderer()
    {
        _wrapped = new NativeClass();
    }
    ~Renderer()
    {
        delete _wrapped;
    }
    operator NativeClass*()
    {
        return _wrapped;
    }
}

现在,我想使用 Wrapperwrapper = new Wrapper() 从 C# 创建一个 Wrapper 实例,并在另一个本机功能包装器中使用它,该包装器驻留在 另一个 程序集中,使用 >Helper.Foo(wrapper) (与另一个程序集中的包装类不直接相关的其他功能没什么奇怪的,IMO):

// Utilities is in another Assembly
public ref class Helper
{
public:
    static Foo(Wrapper ^wrapper)
    {
        // Do something in native code with wrapper->_wrapped
    }
}

隐式用户转换的结果是:

  • 候选函数不可访问

如果我使_wrapped public 它是:

  • 无法访问类中声明的私有成员...

现在,我了解到本机类型可见性是 私有在程序集之外。那么,我应该如何在定义的程序集之外的本机代码中使用包装的实体?我读过 make_public 但是您不能与模板类型一起使用,因此在一般情况下它似乎非常有限。我错过了什么吗?有更正确的解决方案吗?

Suppose I have the following simple wrapper of a NativeClassInstance.

public ref class Wrapper
{
private:
    NativeClass *_wrapped;
public:
    Renderer()
    {
        _wrapped = new NativeClass();
    }
    ~Renderer()
    {
        delete _wrapped;
    }
    operator NativeClass*()
    {
        return _wrapped;
    }
}

Now, I want to create an instance of Wrapper from C# with Wrapper wrapper = new Wrapper() and use it in another native functionalities wrapper that resides in another assembly with Helper.Foo(wrapper) (nothing strange having other functionalities not directly related to the wrapped classes in another assembly, IMO):

// Utilities is in another Assembly
public ref class Helper
{
public:
    static Foo(Wrapper ^wrapper)
    {
        // Do something in native code with wrapper->_wrapped
    }
}

The results with the implicit user conversion is:

  • candidate function(s) not accessible

If I make _wrapped public it is:

  • cannot access private member declared in class ...

Now, I've learnt that native type visibility is private outside of the assembly. So, how I'm supposed to use the wrapped entity in native code outside the assembly it's defined? I've read of make_public but you can't use with template types so it seems very limiting in the general case. Am I missing something? Is there a more correct solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-11-15 21:41:36

我无法使用 make_public 成功公开本机类型,但是我使用的解决方案是将 NativeClass 放入其自己的本机 DLL 中,然后 a) 引用本机来自两个程序集的 DLL; b) 将指针作为 IntPtr 传递给本机类。

在上述场景下,您可以使用诸如之类的属性,而不是使用 operator NativeClass*

property IntPtr WrappedObject {
    IntPtr get() { return IntPtr(_wrapped); }
}

然后在辅助程序集中通过以下方式检索 NativeObject

static void Foo(Wrapper ^wrapper)
{
    NativeObject *_wrapped
        = static_cast<NativeObject*>(wrapper->WrappedObject.ToPointer());
    // ... do something ...
}

I haven't been able to successfully expose native types using make_public, however a solution I have used is to put NativeClass in its own native DLL and then a) reference the native DLL from both assemblies; and b) pass the pointer to the native class around as an IntPtr.

Under the above scenario, instead of having an operator NativeClass* you might use a property such as

property IntPtr WrappedObject {
    IntPtr get() { return IntPtr(_wrapped); }
}

You then retrieve NativeObject in you helper assembly by

static void Foo(Wrapper ^wrapper)
{
    NativeObject *_wrapped
        = static_cast<NativeObject*>(wrapper->WrappedObject.ToPointer());
    // ... do something ...
}
小嗷兮 2024-11-15 21:41:36

如果您使用 make_public,则将 _wrapped 设为公共的解决方案应该可以工作(显然最好改为使用公共访问器)。关于您的评论“我读过 make_public 但您不能与模板类型一起使用,因此在一般情况下它似乎非常有限。”我同意 - 请阅读此处了解我使用的解决方法:
http://social .msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/

更多相关信息:
编译器错误 C2158 的最佳解决方法: make_public 不支持原生模板类型

祝你好运!

If you use make_public, your solution of making _wrapped public should work (it would obviously be best to make a public accessor instead). Regarding your comment "I've read of make_public but you can't use with template types so it seems very limiting in the general case." I agree--read here for the workaround I used:
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/

More related info:
Best workaround for compiler error C2158: make_public does not support native template types

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文