Skip to content

Instantly share code, notes, and snippets.

@sachintha81
Created August 16, 2017 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sachintha81/82f3d6b5974e4b35b650cff4e2179e90 to your computer and use it in GitHub Desktop.
Save sachintha81/82f3d6b5974e4b35b650cff4e2179e90 to your computer and use it in GitHub Desktop.
Get the description of any Enum
public static class EnumHelper<T>
{
public static string GetEnumDescription(string value)
{
Type type = typeof(T);
var name = Enum.GetNames(type).Where(f => f.Equals(value, StringComparison.CurrentCultureIgnoreCase)).Select(d => d).FirstOrDefault();
if (name == null)
{
return string.Empty;
}
var field = type.GetField(name);
var customAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
return customAttribute.Length > 0 ? ((DescriptionAttribute)customAttribute[0]).Description : name;
}
}
@sachintha81
Copy link
Author

If the description is not available, enum itself will be returned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment