12345678910111213141516171819202122 |
- using System.ComponentModel;
- namespace System
- {
- public static class EnumExtension
- {
- public static string GetDescription<T>(this T value) where T : Enum
- {
- var field = value.GetType().GetField(value.ToString());
- if (field == null) return null;
- object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (objs.Length == 0)
- return value.ToString();
- DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
- return descriptionAttribute.Description;
- }
- }
- }
|