C#和C ++类继承混合 [英] C# and C++ class inheritance intermingling

查看:77
本文介绍了C#和C ++类继承混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的程序集,我想将它们放在一起:

I have an interesting stack of assemblies I want to put together:


  1. 通用程序集(C#或C ++-CLI)

  1. Common Assembly (C# or C++-CLI)

public class MyBase
{
public void MethodA()
{ ... }
private void MethodB()
{ ... }
protected virtual MethodC()
{ ... }
}


  • 测试代码程序集(全部C ++-CLI)

  • Test Code Assemblies (all C++-CLI)

    public class MySpecific : public MyBase{
    protected: override MethodC();
    };
    


  • 测试模拟器(C#)

  • Test Simulator (C#)

    MySpecific obj = new MySpecific();
    obj.MethodC();
    


  • 而汇编1可能是C ++-CLI让事情变得更简单,我真的很想将汇编3保持在C#中。这主要是一个练习,看是否可以在两个方向上都可以进行继承,但是我也有一个现实情况,该堆栈很有用。

    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.

    我的第一个问题发现是C ++-CLI程序集无法编译,因为它无法将MyBase识别为类,即使我引用了程序集1以及看起来像是正确的命名空间也是如此。

    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?

    推荐答案

    我认为您必须将MySpecific声明为托管类像这样的类:

    I think you have to declare MySpecific as a managed class like this:

    public ref class MySpecific : public MyBase { ... }
    

    有关详细信息,请参见CLI / C ++文档。未经修改,您将无法使用旧的C ++代码,但我认为您应该牢记的所有内容。

    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.

    这篇关于C#和C ++类继承混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆