C# 和 C++ 类继承混合
我想将一堆有趣的程序集放在一起:
通用程序集(C# 或 C++-CLI)
公共类MyBase { 公共无效方法A() { ... } 私有无效方法B() { ... } 受保护的虚拟方法C() { ... } }
测试代码程序集(所有 C++-CLI)
公共类 MySpecific :公共 MyBase{ 受保护:重写MethodC(); };
测试模拟器 (C#)
MySpecific obj = new MySpecific(); obj.MethodC();
虽然程序集 1 可以是 C++-CLI 以使事情更简单,但我真的很想将程序集 3 保留在 C# 中。 这主要是一个练习,看看是否可以在任一方向上完成继承,但我也有一个实际案例,该堆栈很有用。
我发现的第一个问题是 C++-CLI 程序集无法编译,因为它无法将 MyBase 识别为类,即使我引用了程序集 1 以及看起来正确的命名空间。
如何编写语言可移植的类?
I have an interesting stack of assemblies I want to put together:
Common Assembly (C# or C++-CLI)
public class MyBase { public void MethodA() { ... } private void MethodB() { ... } protected virtual MethodC() { ... } }
Test Code Assemblies (all C++-CLI)
public class MySpecific : public MyBase{ protected: override MethodC(); };
Test Simulator (C#)
MySpecific obj = new MySpecific(); obj.MethodC();
While assembly 1 could be C++-CLI to keep things simpler, I'd really like to keep assembly 3 in C#. This is largely an exercise to see if inheritance could be done in either direction, but I also have a real-world case where this stack is useful.
The first problem I find is that the C++-CLI assembly does not compile because it doesn't recognize MyBase as a class, even though I have a reference to assembly 1 and what looks like the proper namespace.
How do I write classes that are language-portable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您必须将 MySpecific 声明为托管类,如下所示:
有关详细信息,请参阅 CLI/C++ 文档。 如果不进行修改,您将无法使用旧的 C++ 代码,但我认为您想到的一切都应该是可能的。
I think you have to declare MySpecific as a managed class like this:
See the CLI/C++ documentation for details. You will not be able to use old C++ code without modifications, but I think everything you have in mind should be possible.
找到答案:
到目前为止,除了 /clr 开关(C++ 端需要管理)之外似乎还有效。
Found the answer:
That, in addition to the /clr switch (the c++ side needs to be mananged) seems to be working, so far.
这可以正常工作,但如果您使用托管类型(即:从 C# 类继承),则需要使用 C++/CLI 语法。 因此,在这种情况下,2. 中的项目应该看起来更像是:
确保文件使用 /clr 编译良好。
这是描述 C++/CLI 继承的教程。
This will work fine, but you'll need to use the C++/CLI syntax, if you're working with managed types (ie: inheriting from a C# class). So, in this case, the item in 2. should look more like:
Make sure that file is compiled with /clr are well.
Here is a tutorial describing inheritance in C++/CLI.