Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to marshal a C++ enum in C#

I need to create a wrapper between C++ and C#. I have a function very similar to this:

virtual SOMEINTERFACE* MethodName(ATTRIBUTE_TYPE attribType = ATTRIBUTE_TYPE::ATTRIB_STANDARD) = 0;

The enum is declared like this:

enum class ATTRIBUTE_TYPE { 
    ATTRIB_STANDARD, 
    ATTRIB_LENGTH 
};

How do I wrap that ATTRIBUTE_TYPE enum?

like image 603
Rock3rRullz Avatar asked Mar 04 '14 15:03

Rock3rRullz


People also ask

Can you change the value of an enum in C?

You can change default values of enum elements during declaration (if necessary).

Can we have enum inside enum in C?

It is not possible to have enum of enums, but you could represent your data by having the type and cause separately either as part of a struct or allocating certain bits for each of the fields.

Does enum exist C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Do C enums start at 0?

So yes, if you do not specify a start value, it will default to 0.


1 Answers

Your C++ enum is defined like this:

enum class ATTRIBUTE_TYPE { 
    ATTRIB_STANDARD, 
    ATTRIB_LENGTH 
};

By default, enum class types are int sized. Which means that you can translate this to C# like so:

enum ATTRIBUTE_TYPE { 
    ATTRIB_STANDARD, 
    ATTRIB_LENGTH 
};

That's all there is to it. A C# enum is blittable and this C# enum maps exactly on to your C++ enum.

like image 112
David Heffernan Avatar answered Oct 13 '22 02:10

David Heffernan