EnumExtension.cs 622 B

12345678910111213141516171819202122
  1. using System.ComponentModel;
  2. namespace System
  3. {
  4. public static class EnumExtension
  5. {
  6. public static string GetDescription<T>(this T value) where T : Enum
  7. {
  8. var field = value.GetType().GetField(value.ToString());
  9. if (field == null) return null;
  10. object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
  11. if (objs.Length == 0)
  12. return value.ToString();
  13. DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
  14. return descriptionAttribute.Description;
  15. }
  16. }
  17. }