Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class EnumHelper
- {
- public static T GetAttribute<T>(this Enum value) where T : Attribute
- {
- var type = value.GetType();
- var memberInfo = type.GetMember(value.ToString());
- var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
- return (T)attributes[0];
- }
- public static string ToDescription(this Enum value)
- {
- var attribute = value.GetAttribute<DescriptionAttribute>();
- return attribute == null ? value.ToString() : attribute.Description;
- }
- public static List<string> GetAttributeDescriptions<T>() where T : struct, IConvertible
- {
- if (!typeof(T).IsEnum)
- throw new ArgumentException("T must be an enumerated type");
- var items = new List<string>();
- foreach (Enum item in Enum.GetValues(typeof(T)))
- {
- items.Add(item.ToDescription());
- }
- return items;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement