Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how can I refer to a typedef from a C++/CLI class

Tags:

c#

c++-cli

Given the following c++/cli code:

public ref class A
{
public:
    typedef System::String B;
};

How can I get the following C# code to work:

var b = new A.B();

EDIT: if it helps, the typedef is not a hard requirement. My goal was to end up with a type defined once and usable in both worlds.

like image 812
screwnut Avatar asked Oct 01 '22 16:10

screwnut


1 Answers

C# doesn't have typedefs. It does have a form of type aliases made with using declarations, but those don't support your usage (the type alias has to be repeated in every file where it is used, it isn't carried with the containing type).

It's very rare for a language to support consuming a feature from another language, that the first doesn't itself support. And, no surprise, C# doesn't support typedefs declared in C++/CLI any more than it supports typedefs declared in C#.

If you have code that needs this feature, write it in C++/CLI, and call it from C#.

like image 63
Ben Voigt Avatar answered Oct 05 '22 10:10

Ben Voigt