Blending C # and C ++ Class Inheritance

I have an interesting assembly stack that I want to build:

  • General assembly (C # or C ++ - CLI)

    public class MyBase
    {
    public void MethodA()
    { ... }
    private void MethodB()
    { ... }
    protected virtual MethodC()
    { ... }
    }
    
  • Test code subscribers (all C ++ - CLI)

    public class MySpecific : public MyBase{
    protected: override MethodC();
    };
    
  • Test Simulator (C #)

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

While assembly 1 can be C ++ - CLI, to simplify the task, I would really like to save assembly 3 in C #. This is basically an exercise to make sure inheritance can be done in any direction, but I also have a real case where this stack is useful.

The first problem that I find is that the C ++ - CLI assembly does not compile because it does not recognize MyBase as a class, although I have a reference to assembly 1 and what looks like its own namespace.

How to write classes that carry the language?

+5
3

, MySpecific :

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

. CLI/++. ++ , , , , .

+4

:

`#using "..\common\bin\debug\common.dll"`

, /clr- ( ++ ), , .

+3

This will work fine, but you need to use the C ++ / CLI syntax if you work with managed types (i.e.: inherit from a C # class). So, in this case, the element in 2. should look bigger:

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

Make sure the file is compiled with / clr. Good.

Here is a tutorial describing inheritance in C ++ / CLI .

+1
source

All Articles