Extensions.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 WMS.BZModels
  9. {
  10. public static class Extensions
  11. {
  12. #region 获取枚举的描述
  13. /// <summary>
  14. /// 获取枚举值对应的描述
  15. /// </summary>
  16. /// <param name="enumType"></param>
  17. /// <returns></returns>
  18. public static string GetDescription(this System.Enum enumType)
  19. {
  20. var value = enumType.ToString();
  21. if (value.IndexOf(',') > 0)
  22. {
  23. var lists = value.Split(',');
  24. var strings = new List<string>();
  25. foreach (var item in lists)
  26. {
  27. //var itemenum = (WCS.Entity.Protocol.Station.StationStatus)Enum.Parse(typeof(StationStatus), item);
  28. FieldInfo itemEnumInfo = enumType.GetType().GetField(item.Trim());
  29. if (itemEnumInfo != null)
  30. {
  31. DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])itemEnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  32. if (EnumAttributes.Length > 0)
  33. {
  34. strings.Add(EnumAttributes[0].Description);
  35. }
  36. }
  37. else
  38. {
  39. strings.Add(item);
  40. }
  41. }
  42. return string.Join(",", strings.ToArray());
  43. }
  44. else
  45. {
  46. FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
  47. if (EnumInfo != null)
  48. {
  49. DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  50. if (EnumAttributes.Length > 0)
  51. {
  52. return EnumAttributes[0].Description;
  53. }
  54. }
  55. return enumType.ToString();
  56. }
  57. }
  58. /// <summary>
  59. /// 将枚举转为集合
  60. /// </summary>
  61. /// <typeparam name="T"></typeparam>
  62. /// <returns></returns>
  63. public static List<EnumEntity> EnumToList<T>()
  64. {
  65. List<EnumEntity> list = new List<EnumEntity>();
  66. foreach (var e in Enum.GetValues(typeof(T)))
  67. {
  68. EnumEntity m = new EnumEntity();
  69. object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
  70. if (objArr != null && objArr.Length > 0)
  71. {
  72. DescriptionAttribute da = objArr[0] as DescriptionAttribute;
  73. m.text = da.Description;
  74. }
  75. else
  76. {
  77. m.text = e.ToString();
  78. }
  79. m.id = Convert.ToInt32(e);
  80. list.Add(m);
  81. }
  82. return list;
  83. }
  84. public static List<EnumEntity2> EnumToListDesc<T>()
  85. {
  86. List<EnumEntity2> list = new List<EnumEntity2>();
  87. foreach (var e in Enum.GetValues(typeof(T)))
  88. {
  89. EnumEntity2 m = new EnumEntity2();
  90. object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
  91. if (objArr != null && objArr.Length > 0)
  92. {
  93. DescriptionAttribute da = objArr[0] as DescriptionAttribute;
  94. m.text = da.Description;
  95. m.id = da.Description;
  96. }
  97. //else
  98. //{
  99. // m.text = e.ToString();
  100. //}
  101. //m.id = da.Description;
  102. list.Add(m);
  103. }
  104. return list;
  105. }
  106. public static List<EnumEntity2> EnumToListIdDesc<T>()
  107. {
  108. List<EnumEntity2> list = new List<EnumEntity2>();
  109. foreach (var e in Enum.GetValues(typeof(T)))
  110. {
  111. EnumEntity2 m = new EnumEntity2();
  112. object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
  113. if (objArr != null && objArr.Length > 0)
  114. {
  115. DescriptionAttribute da = objArr[0] as DescriptionAttribute;
  116. m.text = da.Description;
  117. }
  118. //else
  119. //{
  120. // m.text = e.ToString();
  121. //}
  122. m.id = e.ToString();
  123. list.Add(m);
  124. }
  125. return list;
  126. }
  127. #endregion
  128. }
  129. /// <summary>
  130. /// 枚举实体
  131. /// </summary>
  132. public class EnumEntity
  133. {
  134. /// <summary>
  135. /// 枚举对象的值
  136. /// </summary>
  137. public int id { set; get; }
  138. /// <summary>
  139. /// 枚举的描述
  140. /// </summary>
  141. public string text { set; get; }
  142. }
  143. public class EnumEntity2
  144. {
  145. /// <summary>
  146. /// 枚举对象的值
  147. /// </summary>
  148. public string id { set; get; }
  149. /// <summary>
  150. /// 枚举的描述
  151. /// </summary>
  152. public string text { set; get; }
  153. }
  154. }