Created
August 16, 2017 22:27
-
-
Save sachintha81/82f3d6b5974e4b35b650cff4e2179e90 to your computer and use it in GitHub Desktop.
Get the description of any Enum
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the description is not available, enum itself will be returned.