Get enum description from enum class, similar get variable name directly.
Last active
October 12, 2016 00:43
-
-
Save pokk/d29dfd51ed72047ebc6abc91c0495827 to your computer and use it in GitHub Desktop.
Get enum description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private enum Country | |
{ | |
[Description("United States")] | |
US, | |
[Description("Dutch")] | |
Dutch, | |
[Description("Japan")] | |
Japan, | |
[Description("France")] | |
France, | |
[Description("Russia")] | |
Russia, | |
} | |
private static string getEnumDescription(Enum value) | |
{ | |
FieldInfo fi = value.GetType().GetField(value.ToString()); | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (attributes != null && attributes.Length > 0) | |
return attributes[0].Description; | |
else | |
return value.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment