在 C++/CLI 项目中导出本机命名空间?
我正在包装一个本机 C++ 库以供 CLR 使用。但是,我遇到了一个奇怪的...问题?
本机库的标头如下所示:
namespace Foo {
class Bar {
public:
Bar();
//etc...
};
}
因此,为了使用这个类,我有自己的类定义:
#include "Foo/Bar.h"
namespace FooNet {
public ref class Bar {
private:
Foo::Bar * m_Impl;
internal:
Bar(Foo::Bar *);
//etc...
};
}
而且,这一切都很好。但是,当我将生成的程序集引用到 C# 项目(例如)并查看对象浏览器时,我注意到它仅包含我的 CLR 类 (FooNet::Barnot) code>)
,还有原生类 (!Foo::Bar
)
我并不是特别热衷于公开本机类,因为它们使用指针和 std::string 以及其他 .NET 不友好的东西,那么有什么方法可以阻止这种情况发生吗?编辑
:我今天学到的东西:
- 对象浏览器显示解决方案中的所有名称空间,而不仅仅是您正在查看的任何项目中的名称空间。
- 本机 C++ 类不在托管程序集中公开。
I am wrapping a native C++ library for consumption by the CLR. However, I'm running into a strange... problem?
The native library's headers look like so:
namespace Foo {
class Bar {
public:
Bar();
//etc...
};
}
So, to consume this class, I have my own class definition:
#include "Foo/Bar.h"
namespace FooNet {
public ref class Bar {
private:
Foo::Bar * m_Impl;
internal:
Bar(Foo::Bar *);
//etc...
};
}
And, that all works great. However, when I reference the resulting assembly into a C# project (for example) and look at the object browser, I notice that it contains not only my CLR classes (FooNet::Bar
), but also the native classes (!Foo::Bar
) too
I'm not particularly enthusiastic about exposing the native classes, since they use pointers and std::string
s and other .NET unfriendly stuff, so is there any way to stop this from happening?
Edit: Things I learned today:
- The object browser shows all namespaces in the solution, not in just whatever project you happen to be looking at.
- Native C++ classes are not exposed in managed assemblies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能的是,为了托管调试器的利益,本机类被列在元数据中,但它们应该被标记为“内部”,并且不能被消费者代码使用。
Most likely the native classes are listed in the metadata for the benefit of managed debuggers, but they should be marked
internal
and not usable by consumer code.