Skip to content

Instantly share code, notes, and snippets.

@tdewilde
Created April 7, 2014 08:23
Show Gist options
  • Select an option

  • Save tdewilde/10016599 to your computer and use it in GitHub Desktop.

Select an option

Save tdewilde/10016599 to your computer and use it in GitHub Desktop.
Extension method to get the Description value from an Enum. If not present, return Enum name.
public static string GetDescription(this Enum enumeration)
{
string value = enumeration.ToString();
Type enumType = enumeration.GetType();
var descAttribute = (DescriptionAttribute[])enumType
.GetField(value)
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return descAttribute.Length > 0 ? descAttribute[0].Description : value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment