Get Enum Description with function
I had a helper function for this at some point, but I lost it..
How can I get the description tag from any enum?
Say I have an enum:
and I wanted to get the description values returned, how would I go about it, regardless of Enum I put into the function?
Preferably I would want to call
MyEnum.GetDescription(MyEnum.First)
or something to return "The First"
9 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Console.WriteLine(MyEnum.First.GetEnumDescription());
I get
"FieldInfo does not contain a definition for 'GetCustomAttribute' and no accessible method 'GetCustomAttribute' accepting first argument of type 'FieldInfo' could be found"
Weird. I'm using .net 6
Whelp, oops. My IDE just lagged out and forgot to import references. My bad
new and improved version
it lets you specify the "no description" thing, in case its needed :p
Ah fair enough, thanks
also
GetEnumDescription
was too verbose, removed the "Enum" part (as the generic only works for enums anyways)Fair enough, thanks a bunch!