Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

209 řádky
7.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace JT809.Protocol.JT809Extensions
  7. {
  8. /// <summary>
  9. /// 枚举扩展
  10. /// </summary>
  11. public static class JT809EnumExtensions
  12. {
  13. /// <summary>
  14. /// 转为整型
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="t"></param>
  18. /// <returns></returns>
  19. public static int ToValue<T>(this T t) where T : struct
  20. {
  21. return Convert.ToInt32(t);
  22. }
  23. /// <summary>
  24. /// 转为Byte
  25. /// </summary>
  26. /// <typeparam name="T"></typeparam>
  27. /// <param name="t"></param>
  28. /// <returns></returns>
  29. public static byte ToByteValue<T>(this T t) where T : struct
  30. {
  31. return Convert.ToByte(t);
  32. }
  33. /// <summary>
  34. /// 转为整型
  35. /// </summary>
  36. /// <typeparam name="T"></typeparam>
  37. /// <param name="t"></param>
  38. /// <returns></returns>
  39. public static string ToValueString<T>(this T t) where T : struct
  40. {
  41. return Convert.ToInt32(t).ToString();
  42. }
  43. /// <summary>
  44. /// 字符转枚举
  45. /// </summary>
  46. /// <typeparam name="T"></typeparam>
  47. /// <param name="value"></param>
  48. /// <returns></returns>
  49. public static T ToEnum<T>(this string value) where T : struct
  50. {
  51. return (T)Enum.Parse(typeof(T), value);
  52. }
  53. /// <summary>
  54. /// 获取枚举字符串
  55. /// </summary>
  56. /// <param name="valueEnum"></param>
  57. public static string GetName(this Enum valueEnum)
  58. {
  59. return valueEnum.ToString();
  60. }
  61. /// <summary>
  62. /// 获取DescriptionAttribute特性枚举值的描述
  63. /// </summary>
  64. /// <param name="value"></param>
  65. /// <returns></returns>
  66. public static string GetDescription(this Enum value)
  67. {
  68. var attribute = value.GetAttribute<DescriptionAttribute>();
  69. return attribute == null ? value.ToString() : attribute.Description;
  70. }
  71. /// <summary>
  72. /// 验证是否是枚举类型
  73. /// </summary>
  74. /// <typeparam name="TEnum"></typeparam>
  75. /// <param name="enumValue"></param>
  76. /// <returns></returns>
  77. public static bool IsEnumValid<TEnum>(this int enumValue)
  78. {
  79. return Enum.IsDefined(typeof(TEnum), enumValue);
  80. }
  81. /// <summary>
  82. /// 获取DescriptionAttribute特性枚举及描述
  83. /// </summary>
  84. /// <param name="value"></param>
  85. /// <returns></returns>
  86. public static Dictionary<string, string> GetDescriptionAttributeDictionary(this Enum value)
  87. {
  88. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  89. var fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
  90. foreach (var fi in fields)
  91. {
  92. DescriptionAttribute attr = Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute), false) as DescriptionAttribute;
  93. dictionary.Add(fi.Name, attr != null ? attr.Description : "");
  94. }
  95. return dictionary;
  96. }
  97. /// <summary>
  98. /// 获取DisplayNameAttribute特性枚举值的描述
  99. /// </summary>
  100. /// <param name="value">枚举值</param>
  101. /// <returns></returns>
  102. public static string GetDisplayName(this Enum value)
  103. {
  104. var attribute = value.GetAttribute<DisplayNameAttribute>();
  105. return attribute == null ? value.ToString() : attribute.DisplayName;
  106. }
  107. /// <summary>
  108. /// 获取DisplayNameAttribute特性枚举及描述
  109. /// </summary>
  110. /// <param name="value"></param>
  111. /// <returns></returns>
  112. public static Dictionary<string, string> GetDisplayNameAttributeDictionary(this Enum value)
  113. {
  114. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  115. var fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
  116. foreach (var fi in fields)
  117. {
  118. DisplayNameAttribute attr = Attribute.GetCustomAttribute(fi, typeof(DisplayNameAttribute), false) as DisplayNameAttribute;
  119. dictionary.Add(fi.Name, attr != null ? attr.DisplayName : "");
  120. }
  121. return dictionary;
  122. }
  123. /// <summary>
  124. /// 获取枚举对应特性
  125. /// </summary>
  126. /// <typeparam name="T"></typeparam>
  127. /// <param name="value"></param>
  128. /// <returns></returns>
  129. public static T GetAttribute<T>(this Enum value) where T : Attribute
  130. {
  131. try
  132. {
  133. var type = value.GetType();
  134. var memberInfo = type.GetMember(value.ToString());
  135. var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
  136. return (T)attributes[0];
  137. }
  138. catch
  139. {
  140. return default;
  141. }
  142. }
  143. /// <summary>
  144. /// 根据值获取对应枚举类型集合
  145. /// </summary>
  146. /// <typeparam name="T">具体枚举类型</typeparam>
  147. /// <param name="value">枚举值</param>
  148. /// <param name="digit">位数(8,16,32)</param>
  149. /// <returns></returns>
  150. public static IEnumerable<T> GetEnumTypes<T>(this int value,int digit) where T : Enum
  151. {
  152. return GetEnumTypes<T>(value, digit,true);
  153. }
  154. /// <summary>
  155. /// 根据值获取对应枚举类型集合
  156. /// </summary>
  157. /// <typeparam name="T">具体枚举类型</typeparam>
  158. /// <param name="value">枚举值</param>
  159. /// <param name="digit">位数(8,16,32)</param>
  160. /// <param name="ignoreUnknown">是否忽略未知数据</param>
  161. /// <returns></returns>
  162. public static IEnumerable<T> GetEnumTypes<T>(this int value, int digit,bool ignoreUnknown) where T : Enum
  163. {
  164. List<T> values = new List<T>();
  165. for (int i = 0; i < digit; i++)
  166. {
  167. if (Math.Pow(2, i) <= value) continue;
  168. values.Add((T)Enum.ToObject(typeof(T), (int)Math.Pow(2, i - 1)));
  169. value = value - (int)Math.Pow(2, i - 1);
  170. i = 0;
  171. if (value <= 0) break;
  172. }
  173. if (ignoreUnknown)
  174. {
  175. List<T> results = new List<T>();
  176. foreach (var item in values)
  177. {
  178. foreach (string itemChild in Enum.GetNames(typeof(T)))
  179. {
  180. if (item.ToString() == itemChild)
  181. {
  182. results.Add(item);
  183. break;
  184. }
  185. }
  186. }
  187. return results;
  188. }
  189. else
  190. {
  191. return values;
  192. }
  193. }
  194. }
  195. }