EnumExtension.cs 987 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. namespace DxHelper
  10. {
  11. /// <summary>
  12. /// 枚举扩展
  13. /// </summary>
  14. public static class EnumExtension
  15. {
  16. /// <summary>
  17. /// 获取枚举描述
  18. /// </summary>
  19. /// <param name="enumType">枚举类型</param>
  20. /// <returns></returns>
  21. public static string GetEnumDisplayName(this Enum enumType)
  22. {
  23. try
  24. {
  25. Type type = enumType.GetType();
  26. var field = type.GetField(Enum.GetName(type, enumType));
  27. string displayName = field.GetCustomAttribute<DisplayAttribute>().Name;
  28. return displayName;
  29. }
  30. catch
  31. {
  32. return string.Empty;
  33. }
  34. }
  35. }
  36. }