EnumHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OASystem.Domain.Enums
  10. {
  11. public static class EnumHelper
  12. {
  13. //--extension
  14. /// <summary>
  15. /// 获取当前枚举描述
  16. /// </summary>
  17. /// <param name="enumValue"></param>
  18. /// <returns></returns>
  19. public static string GetEnumDescription(this Enum enumValue)
  20. {
  21. try
  22. {
  23. Type type = enumValue.GetType();
  24. MemberInfo[] memInfo = type.GetMember(enumValue.ToString());
  25. if (null != memInfo && memInfo.Length > 0)
  26. {
  27. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  28. if (null != attrs && attrs.Length > 0)
  29. return ((DescriptionAttribute)attrs[0]).Description;
  30. }
  31. return enumValue.ToString();
  32. }
  33. catch (Exception)
  34. {
  35. return "";
  36. }
  37. }
  38. //根据值获取枚举方法
  39. public static Enum GetEnumByValue(Type enumType, string value)
  40. {
  41. return Enum.Parse(enumType, value) as Enum;
  42. }
  43. /// <summary>
  44. /// 根据Key获取枚举描述
  45. /// </summary>
  46. /// <param name="en"></param>
  47. /// <returns></returns>
  48. public static string GetDescription(this System.Enum en)
  49. {
  50. Type type = en.GetType();
  51. MemberInfo[] memInfo = type.GetMember(en.ToString());
  52. if (memInfo != null && memInfo.Length > 0)
  53. {
  54. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  55. if (attrs != null && attrs.Length > 0)
  56. return ((DescriptionAttribute)attrs[0]).Description;
  57. }
  58. return en.ToString();
  59. }
  60. /// <summary>
  61. /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合
  62. /// </summary>
  63. /// <param name="enumType"></param>
  64. /// <returns></returns>
  65. public static NameValueCollection GetNVCFromEnumValue(Type enumType)
  66. {
  67. NameValueCollection nvc = new NameValueCollection();
  68. Type typeDescription = typeof(DescriptionAttribute);
  69. System.Reflection.FieldInfo[] fields = enumType.GetFields();
  70. string strText = string.Empty;
  71. string strValue = string.Empty;
  72. foreach (FieldInfo field in fields)
  73. {
  74. if (field.FieldType.IsEnum)
  75. {
  76. //strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
  77. strValue = (enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null) as int?)?.ToString() ?? string.Empty;
  78. object[] arr = field.GetCustomAttributes(typeDescription, true);
  79. if (arr.Length > 0)
  80. {
  81. DescriptionAttribute aa = (DescriptionAttribute)arr[0];
  82. strText = aa.Description;
  83. }
  84. else
  85. {
  86. strText = "";
  87. }
  88. nvc.Add(strValue, strText);
  89. }
  90. }
  91. return nvc;
  92. }
  93. /// <summary>
  94. /// 获取枚举的所有值及其对应的描述
  95. /// </summary>
  96. /// <param name="enumType"></param>
  97. /// <param name="enumKeys"></param>
  98. /// <returns></returns>
  99. public static Dictionary<string, string> GetEnumDescriptions(Type enumType, string[]? enumKeys = null)
  100. {
  101. var descriptions = new Dictionary<string, string>();
  102. if (!enumType.IsEnum)
  103. {
  104. return descriptions;
  105. //throw new ArgumentException("Type must be an enum", nameof(enumType));
  106. }
  107. foreach (var field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
  108. {
  109. var descriptionAttribute = field.GetCustomAttribute<DescriptionAttribute>();
  110. var description = descriptionAttribute?.Description ?? field.Name;
  111. var name = field.Name;
  112. if (enumKeys != null && enumKeys.Any())
  113. {
  114. if (!enumKeys.Contains(name))
  115. {
  116. descriptions[name] = description;
  117. }
  118. }
  119. else {
  120. descriptions[name] = description;
  121. }
  122. }
  123. return descriptions;
  124. }
  125. }
  126. }