EnumExtension.cs 1019 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using XdCxRhDW.Repostory.Model;
  10. namespace DxHelper
  11. {
  12. /// <summary>
  13. /// 枚举扩展
  14. /// </summary>
  15. public static class EnumExtension
  16. {
  17. /// <summary>
  18. /// 获取枚举描述
  19. /// </summary>
  20. /// <param name="enumType">枚举类型</param>
  21. /// <returns></returns>
  22. public static string GetEnumDisplayName(this Enum enumType)
  23. {
  24. try
  25. {
  26. Type type = enumType.GetType();
  27. var field = type.GetField(Enum.GetName(type, enumType));
  28. string displayName = field.GetCustomAttribute<DisplayAttribute>().Name;
  29. return displayName;
  30. }
  31. catch
  32. {
  33. return string.Empty;
  34. }
  35. }
  36. }
  37. }