EnumExtension.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace IoTIntegrationPlatform.Common
  9. {
  10. /// <summary>
  11. /// 枚举对象操作
  12. /// </summary>
  13. public static class EnumExtension
  14. {
  15. /// <summary>
  16. /// 获取枚举的描述信息
  17. /// </summary>
  18. public static string GetDescription(this Enum em)
  19. {
  20. Type type = em.GetType();
  21. FieldInfo fd = type.GetField(em.ToString());
  22. if (fd == null)
  23. {
  24. return string.Empty;
  25. }
  26. object[] attrs = fd.GetCustomAttributes(typeof(DescriptionAttribute), false);
  27. string name = string.Empty;
  28. foreach (DescriptionAttribute attr in attrs)
  29. {
  30. name = attr.Description;
  31. }
  32. return name;
  33. }
  34. /// <summary>
  35. /// 获取枚举值描述
  36. /// </summary>
  37. /// <param name="value"></param>
  38. /// <returns></returns>
  39. public static string GetEnumDescription(this Enum value)
  40. {
  41. FieldInfo fi = value.GetType().GetField(value.ToString());
  42. DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  43. if (attributes != null && attributes.Length > 0)
  44. {
  45. return attributes[0].Description;
  46. }
  47. else
  48. {
  49. return value.ToString();
  50. }
  51. }
  52. ///// <summary>
  53. ///// 获取枚举值描述(无作用)
  54. ///// </summary>
  55. ///// <typeparam name="TEnum"></typeparam>
  56. ///// <param name="value"></param>
  57. ///// <returns></returns>
  58. //public static string GetDescription<TEnum>(this TEnum value) where TEnum : struct
  59. //{
  60. // return value.ToString(); // 默认返回枚举值的名称,这通常是最好的情况。如果枚举值没有自定义描述属性,将返回名称。
  61. //}
  62. /// <summary>
  63. /// 根据值获取枚举方法
  64. /// </summary>
  65. /// <param name="enumType"></param>
  66. /// <param name="value"></param>
  67. /// <returns></returns>
  68. public static Enum GetEnumByValue(Type enumType, string value)
  69. {
  70. return Enum.Parse(enumType, value) as Enum;
  71. }
  72. /// <summary>
  73. /// 获取枚举列表
  74. /// </summary>
  75. /// <param name="type"></param>
  76. /// <returns></returns>
  77. public static List<KeyValuePair<string, int>> GetEnumArray(this Type type)
  78. {
  79. var result = new List<KeyValuePair<string, int>>();
  80. if (type.IsEnum)
  81. {
  82. var values = Enum.GetValues(type);
  83. foreach (var item in values)
  84. {
  85. result.Add(new KeyValuePair<string, int>(((Enum)item).GetDescription(), (int)item));
  86. }
  87. }
  88. return result;
  89. }
  90. public static List<KeyValuePair<string, int>> GetEnumArray(this Type type, params Enum[] notinValue)
  91. {
  92. var result = new List<KeyValuePair<string, int>>();
  93. if (type.IsEnum)
  94. {
  95. var values = Enum.GetValues(type);
  96. foreach (var item in values)
  97. {
  98. if (notinValue.Contains((Enum)item))
  99. {
  100. continue;
  101. }
  102. result.Add(new KeyValuePair<string, int>(((Enum)item).GetDescription(), (int)item));
  103. }
  104. }
  105. return result;
  106. }
  107. }
  108. }