纽威
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.

111 lines
3.2 KiB

3 years ago
3 years ago
3 years 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. public static string GetDescription<Attr>(this Enum em) where Attr : AttrDescription
  45. {
  46. Type enumType = em.GetType();
  47. var name = Enum.GetName(enumType, em);
  48. if (name == null)
  49. return string.Empty;
  50. object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(Attr), false);
  51. if (objs == null || objs.Length == 0)
  52. return string.Empty;
  53. Attr attr = objs[0] as Attr;
  54. return attr.Description;
  55. }
  56. public static string GetDescription(this Enum em)
  57. {
  58. Type enumType = em.GetType();
  59. var name = Enum.GetName(enumType, em);
  60. if (name == null)
  61. return string.Empty;
  62. object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
  63. if (objs == null || objs.Length == 0)
  64. return string.Empty;
  65. System.ComponentModel.DescriptionAttribute attr = objs[0] as System.ComponentModel.DescriptionAttribute;
  66. return attr.Description;
  67. }
  68. }
  69. #region 用于枚举的特性类
  70. public class AttrDescription : Attribute
  71. {
  72. private string _showName;
  73. public AttrDescription(string desc)
  74. {
  75. _showName = desc;
  76. }
  77. public string Description
  78. {
  79. get
  80. {
  81. return _showName;
  82. }
  83. }
  84. }
  85. public class DBValue : AttrDescription
  86. {
  87. public DBValue(string desc)
  88. : base(desc)
  89. {
  90. }
  91. }
  92. //public class DescriptionEn : AttrDescription
  93. //{
  94. // public DescriptionEn(string desc)
  95. // : base(desc)
  96. // {
  97. // }
  98. //}
  99. #endregion
  100. }