EnumExtension.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Resources;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. /// <summary>
  11. /// 枚举扩展
  12. /// </summary>
  13. public static class EnumExtension
  14. {
  15. /// <summary>
  16. /// 获取枚举描述
  17. /// </summary>
  18. /// <param name="enumType">枚举类型</param>
  19. /// <returns></returns>
  20. public static string GetEnumDisplayName(this Enum enumType)
  21. {
  22. try
  23. {
  24. Type type = enumType.GetType();
  25. var field = type.GetField(Enum.GetName(type, enumType));
  26. var attr = field.GetCustomAttribute<DisplayAttribute>();
  27. var attr2 = field.GetCustomAttribute<DescriptionAttribute>();
  28. if (attr == null && attr2 == null)
  29. {
  30. return enumType.ToString();
  31. }
  32. else if (attr == null && attr2 != null)
  33. {
  34. if (string.IsNullOrWhiteSpace(attr2.Description))
  35. return enumType.ToString();
  36. else
  37. return attr2.Description;
  38. }
  39. else if (attr != null && attr2 != null)
  40. {
  41. if (!string.IsNullOrWhiteSpace(attr.Name))
  42. return attr.Name;
  43. if (!string.IsNullOrWhiteSpace(attr2.Description))
  44. return attr2.Description;
  45. return enumType.ToString();
  46. }
  47. else
  48. {
  49. if (string.IsNullOrWhiteSpace(attr.Name))
  50. return enumType.ToString();
  51. else
  52. return attr.Name;
  53. }
  54. }
  55. catch
  56. {
  57. return enumType.ToString();
  58. }
  59. }
  60. public static T GetEnumByDisplayName<T>(this string displayName) where T : struct
  61. {
  62. Type type = typeof(T);
  63. string[] names = Enum.GetNames(type);
  64. string[] array = names;
  65. foreach (string text in array)
  66. {
  67. DisplayAttribute customAttribute = type.GetField(text).GetCustomAttribute<DisplayAttribute>();
  68. DescriptionAttribute da = type.GetField(text).GetCustomAttribute<DescriptionAttribute>();
  69. T result;
  70. if (customAttribute == null && da == null)
  71. {
  72. if (text == displayName && Enum.TryParse(text, out result))
  73. {
  74. return result;
  75. }
  76. continue;
  77. }
  78. string a;
  79. if (customAttribute != null)
  80. {
  81. if (customAttribute.ResourceType == null && string.IsNullOrEmpty(customAttribute.Name))
  82. {
  83. a = text;
  84. }
  85. else if (customAttribute.ResourceType == null)
  86. {
  87. a = (string.IsNullOrEmpty(customAttribute.Name) ? text : customAttribute.Name);
  88. }
  89. else
  90. {
  91. ResourceManager resourceManager = new ResourceManager(customAttribute.ResourceType);
  92. a = resourceManager.GetString(customAttribute.Name);
  93. }
  94. }
  95. else
  96. {
  97. if (da == null && string.IsNullOrEmpty(da.Description))
  98. {
  99. a = text;
  100. }
  101. else
  102. {
  103. a = (string.IsNullOrEmpty(da.Description) ? text : da.Description);
  104. }
  105. }
  106. if (a == displayName && System.Enum.TryParse(text, out result))
  107. {
  108. return result;
  109. }
  110. }
  111. throw new Exception($"{displayName}转换为枚举{typeof(T)}失败");
  112. }
  113. }