You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.5 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.ComponentModel;
  8. namespace ICSSoft.Common
  9. {
  10. public static class EnumHelper
  11. {
  12. #region 获取枚举注解
  13. /// <summary>
  14. /// 判单枚举中是否包含该枚举描述
  15. /// </summary>
  16. /// <param name="enumType"></param>
  17. /// <param name="desc"></param>
  18. /// <returns></returns>
  19. public static bool HasDescriptions(Type enumType,string desc)
  20. {
  21. //if (enumType != null && enumType.IsEnum)
  22. //{
  23. // FieldInfo[] fields = enumType.GetFields();
  24. // for (int i = 1; i < fields.Length; ++i)
  25. // {
  26. // //object fieldValue = Enum.Parse(enumType, fields[i].Name);
  27. // object[] attrs = fields[i].GetCustomAttributes(true);
  28. // foreach (object attr in attrs)
  29. // {
  30. // if (typeof(DescriptionAttribute).IsAssignableFrom(attr.GetType()) && ((DescriptionAttribute)attr).Description.Equals(desc))
  31. // {
  32. // return true;
  33. // }
  34. // }
  35. // }
  36. //}
  37. if (NewTransType.list.Contains(desc))
  38. {
  39. return true;
  40. }
  41. return false;
  42. }
  43. #endregion
  44. #region 获取枚举注解DBValue
  45. /// <summary>
  46. /// 判单枚举中是否包含该枚举描述
  47. /// </summary>
  48. /// <param name="enumType"></param>
  49. /// <param name="desc"></param>
  50. /// <returns></returns>
  51. public static string GetDBValue(Type enumType, string desc)
  52. {
  53. if (enumType != null && enumType.IsEnum)
  54. {
  55. FieldInfo[] fields = enumType.GetFields();
  56. for (int i = 1; i < fields.Length; ++i)
  57. {
  58. //object fieldValue = Enum.Parse(enumType, fields[i].Name);
  59. object[] attrs = fields[i].GetCustomAttributes(true);
  60. bool match = false;
  61. foreach (object attr in attrs)
  62. {
  63. if (typeof(DescriptionAttribute).IsAssignableFrom(attr.GetType()) && ((DescriptionAttribute)attr).Description.Equals(desc))
  64. {
  65. match = true;
  66. continue;
  67. }
  68. if (match) return ((ICSSoft.Common.DBValue)attr).Description;
  69. }
  70. }
  71. }
  72. return "";
  73. }
  74. #endregion
  75. public static string GetDescription<Attr>(this Enum em) where Attr : AttrDescription
  76. {
  77. Type enumType = em.GetType();
  78. var name = Enum.GetName(enumType, em);
  79. if (name == null)
  80. return string.Empty;
  81. object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(Attr), false);
  82. if (objs == null || objs.Length == 0)
  83. return string.Empty;
  84. Attr attr = objs[0] as Attr;
  85. return attr.Description;
  86. }
  87. public static string GetDescription(this Enum em)
  88. {
  89. Type enumType = em.GetType();
  90. var name = Enum.GetName(enumType, em);
  91. if (name == null)
  92. return string.Empty;
  93. object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
  94. if (objs == null || objs.Length == 0)
  95. return string.Empty;
  96. System.ComponentModel.DescriptionAttribute attr = objs[0] as System.ComponentModel.DescriptionAttribute;
  97. return attr.Description;
  98. }
  99. }
  100. #region 用于枚举的特性类
  101. public class AttrDescription : Attribute
  102. {
  103. private string _showName;
  104. public AttrDescription(string desc)
  105. {
  106. _showName = desc;
  107. }
  108. public string Description
  109. {
  110. get
  111. {
  112. return _showName;
  113. }
  114. }
  115. }
  116. public class DBValue : AttrDescription
  117. {
  118. public DBValue(string desc)
  119. : base(desc)
  120. {
  121. }
  122. }
  123. //public class DescriptionEn : AttrDescription
  124. //{
  125. // public DescriptionEn(string desc)
  126. // : base(desc)
  127. // {
  128. // }
  129. //}
  130. #endregion
  131. }