Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

sachintha81 commented Aug 16, 2017

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
You can’t perform that action at this time.