EnumExtensions.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:EnumExtensions
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/17 10:43:13
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System.ComponentModel;
  8. using System.Reflection;
  9. namespace JBIM.Common
  10. {
  11. public static class EnumExtensions
  12. {
  13. /// <summary>
  14. /// 获取指定枚举类型的描述值
  15. /// </summary>
  16. /// <typeparam name="T">枚举类型</typeparam>
  17. /// <returns></returns>
  18. public static string GetDescription<T>(this T t) where T : struct
  19. {
  20. string description = t.ToString();
  21. FieldInfo fieldInfo = t.GetType().GetField(description);
  22. object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  23. if (attributes.Length > 0)
  24. {
  25. DescriptionAttribute info = attributes[0] as DescriptionAttribute;
  26. if (info != null)
  27. {
  28. description = info.Description;
  29. }
  30. }
  31. return description;
  32. }
  33. }
  34. }