How to get name of an enum item in C++ CLI?

c++-cli, enums, visual-c++, visual-studio-2012

Solution

You'll need to use the `enum class` keyword to declare a managed enum type:

public enum class EWeapons
{
    Fist = 0   
};
...
System::String^ Name = Enum::GetName(EWeapons::typeid, (Object^)0);

Do beware a trouble-spot in later versions of Visual Studio (VS2012 and up), the C++11 language specification has adopted the `enum class` keyword as well. Along with other C++/CLI keywords like `override` and `nullptr`. That's a pretty nasty problem for `enum class`, the C++/CLI compiler does distinguish between native enums and managed enum types. Managed enums end up in the metadata, native enums don't. And of course Enum::GetName() cannot work for native enums.

You must use an accessibility keyword (`public` or `private`) to declare a managed enum type. Not valid on native enums, the only way the compiler can tell the difference.

Problem

Already I have the following code but the variable `System::String^ Name_` is `nullptr`: ``` enum EWeapons { Fist = 0 } System::String^ Name_ = Enum::GetName( EWeapons::typeid,0) ```

Original source