Insert enumeration definition name into string

If I have an enumeration:

public enum MyEnum { Element1 = 1, Element2, Element3, Element4 } 

How could I do MyEnum before String() in code

I know that I can overlay any Enum value on sting as on MyEnum.Element1.ToString() , but how can I specify the definition / name of Enum in a string?

I want to do something like this:

MyEnum.ToString()

+4
source share
2 answers

Depending on your use case, I think you might be better off using the display name attribute, like here . Representing an enumeration line is often not quite what you want to display, and you will need to update the code in different places if you want this line to change.

+3
source

As @shsmith said, use:

 typeof(MyEnum).Name 

But, unlike it, do not use:

 MyEnum.GetType().Name 

Since MyEnum is not static and therefore cannot call this method.
You can use GetType() for a specific element, for example:

 MyEnum.Element1.GetType().Name //=MyEnum 
+1
source

All Articles