EnumExtensions.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace FWindSoft.SystemExtensions
  7. {
  8. /// <summary>
  9. /// 枚举类型扩展显示类
  10. /// </summary>
  11. public static class EnumExtensions
  12. {
  13. /// <summary>
  14. /// 获取指定枚举类型的描述值
  15. /// </summary>
  16. /// <typeparam name="T">枚举类型</typeparam>
  17. /// <param name="en">给定枚举</param>
  18. /// <returns></returns>
  19. public static string GetDisplay<T>(this T t) where T :struct
  20. {
  21. string display = t.ToString();
  22. FieldInfo fieldInfo = t.GetType().GetField(display);
  23. object[] attributes = fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false);
  24. if (attributes.Length > 0)
  25. {
  26. DescriptionAttribute info = attributes[0] as DescriptionAttribute;
  27. if (info != null)
  28. {
  29. display = info.Description;
  30. }
  31. }
  32. return display;
  33. }
  34. /// <summary>
  35. /// 获取枚举的条目列表
  36. /// </summary>
  37. /// <typeparam name="T">枚举类型</typeparam>
  38. /// <returns></returns>
  39. public static List<EnumItem<T>> GetEnumItems<T>() where T : struct
  40. {
  41. List<EnumItem<T>> items = new List<EnumItem<T>>();
  42. FieldInfo[] fieldInfos = typeof (T).GetFields();
  43. foreach (FieldInfo field in fieldInfos)
  44. {
  45. if (field.FieldType == typeof (T))
  46. {
  47. object[] attributes = field.GetCustomAttributes(typeof (DescriptionAttribute), false);
  48. if (attributes.Length > 0)
  49. {
  50. DescriptionAttribute info = attributes[0] as DescriptionAttribute;
  51. if (info != null)
  52. {
  53. T tValue = (T) Convert.ChangeType(field.GetValue(field), field.FieldType);
  54. items.Add(new EnumItem<T>(tValue, info.Description));
  55. }
  56. }
  57. }
  58. }
  59. return items;
  60. }
  61. /// <summary>
  62. /// 根据指定描述获取枚举条目
  63. /// </summary>
  64. /// <typeparam name="T">枚举类型</typeparam>
  65. /// <param name="display">枚举类型描述</param>
  66. /// <returns></returns>
  67. public static EnumItem<T> GetEnumItem<T>(string display) where T : struct
  68. {
  69. FieldInfo[] fieldInfos = typeof (T).GetFields();
  70. foreach (FieldInfo field in fieldInfos)
  71. {
  72. if (field.FieldType == typeof (T))
  73. {
  74. object[] attributes = field.GetCustomAttributes(typeof (DescriptionAttribute), false);
  75. if (attributes.Length > 0)
  76. {
  77. DescriptionAttribute info = attributes[0] as DescriptionAttribute;
  78. if (info != null && info.Description == display)
  79. {
  80. T tValue = (T)Convert.ChangeType(field.GetValue(field), field.FieldType);
  81. return new EnumItem<T>(tValue, info.Description);
  82. }
  83. }
  84. else
  85. {
  86. if (field.Name == display)
  87. {
  88. T tValue = (T)Convert.ChangeType(field.GetValue(field), field.FieldType);
  89. return new EnumItem<T>(tValue, display);
  90. }
  91. }
  92. }
  93. }
  94. return null;
  95. }
  96. }
  97. /// <summary>
  98. /// 枚举描述类
  99. /// </summary>
  100. /// <typeparam name="T">枚举类型</typeparam>
  101. public class EnumItem<T> where T : struct
  102. {
  103. private T m_Item;
  104. private string m_Display;
  105. public EnumItem(T item)
  106. {
  107. this.m_Item = item;
  108. this.m_Display = m_Item.GetDisplay();
  109. }
  110. public EnumItem(T item, string display)
  111. {
  112. this.m_Item = item;
  113. this.m_Display = display;
  114. }
  115. public EnumItem(string display)
  116. {
  117. this.m_Item = EnumExtensions.GetEnumItem<T>(display).Item;
  118. this.m_Display = display;
  119. }
  120. public T Item
  121. {
  122. set { m_Item = value; }
  123. get { return m_Item; }
  124. }
  125. public string Display
  126. {
  127. set { m_Display = value; }
  128. get { return m_Display; }
  129. }
  130. public override bool Equals(object obj)
  131. {
  132. bool flag = false;
  133. if (obj == null)
  134. return flag;
  135. if (this.GetType() == obj.GetType())
  136. {
  137. flag = this.Item.Equals(((EnumItem<T>) obj).Item);
  138. }
  139. return flag;
  140. }
  141. public override int GetHashCode()
  142. {
  143. return m_Item.GetHashCode();
  144. }
  145. }
  146. }