EnumExtension.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. namespace TrendChartSDK
  6. {
  7. /// <summary>
  8. /// 枚举扩展方法
  9. /// </summary>
  10. public static class EnumExtension
  11. {
  12. /// <summary>
  13. /// 缓存
  14. /// </summary>
  15. private static Dictionary<string, string> _dictionaryCache = new Dictionary<string, string>();
  16. /// <summary>
  17. /// 返回枚举项的描述信息。
  18. /// </summary>
  19. /// <param name="value">要获取描述信息的枚举项。</param>
  20. /// <returns>枚举想的描述信息。</returns>
  21. public static string GetEnumDescription(this Enum value)
  22. {
  23. Type enumType = value.GetType();
  24. // 获取枚举常数名称。
  25. string name = Enum.GetName(enumType, value);
  26. if (name != null)
  27. {
  28. // 获取枚举字段。
  29. FieldInfo fieldInfo = enumType.GetField(name);
  30. if (fieldInfo != null)
  31. {
  32. // 获取描述的属性。
  33. DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
  34. typeof(DescriptionAttribute), false) as DescriptionAttribute;
  35. if (attr != null)
  36. {
  37. return attr.Description;
  38. }
  39. }
  40. }
  41. return null;
  42. }
  43. /// <summary>
  44. /// 获取枚举描述信息
  45. /// </summary>
  46. /// <param name="en"></param>
  47. /// <returns></returns>
  48. public static string GetEnumText(this System.Enum en)
  49. {
  50. string enString = string.Empty;
  51. if (null == en)
  52. return enString;
  53. string key = en.ToString() + "__KEY__";
  54. if (_dictionaryCache.ContainsKey(key))
  55. {
  56. enString = _dictionaryCache[key];
  57. }
  58. else
  59. {
  60. string tableName = en.ToString();
  61. var fieldInfo = en.GetType().GetField(tableName);
  62. var attributes = (TextAttribute[])fieldInfo.GetCustomAttributes(typeof(TextAttribute), false);
  63. if (attributes.Length > 0)
  64. {
  65. enString = attributes[0].Value;
  66. if (!_dictionaryCache.ContainsKey(key))
  67. {
  68. _dictionaryCache.Add(key, enString);
  69. }
  70. else
  71. {
  72. _dictionaryCache[key] = enString;
  73. }
  74. }
  75. }
  76. return enString;
  77. }
  78. /// <summary>
  79. /// 获取彩种对应数据库表名称
  80. /// </summary>
  81. /// <param name="value">彩种枚举类型</param>
  82. /// <returns></returns>
  83. public static string GetLotteryTableName(this Enum value)
  84. {
  85. if (value == null)
  86. {
  87. throw new ArgumentException("value");
  88. }
  89. string tableName = String.Empty;
  90. var fieldInfo = value.GetType().GetField(value.ToString());
  91. var attributes = (TableNameAttribute[])fieldInfo.GetCustomAttributes(typeof(TableNameAttribute), false);
  92. if (attributes.Length > 0)
  93. {
  94. tableName = attributes[0].TableName;
  95. }
  96. return tableName;
  97. }
  98. /// <summary>
  99. /// 获取彩种对应CId
  100. /// </summary>
  101. /// <param name="value">彩种枚举类型</param>
  102. /// <returns></returns>
  103. public static int GetLotteryCId(this Enum value)
  104. {
  105. if (value == null)
  106. {
  107. throw new ArgumentException("value");
  108. }
  109. int code = -1;
  110. var fieldInfo = value.GetType().GetField(value.ToString());
  111. var attributes = (LotteryCIdAttribute[])fieldInfo.GetCustomAttributes(typeof(LotteryCIdAttribute), false);
  112. if (attributes.Length > 0)
  113. {
  114. code = attributes[0].CId;
  115. }
  116. return code;
  117. }
  118. /// <summary>
  119. /// 遍历枚举对象的所有元素
  120. /// </summary>
  121. /// <typeparam name="T">枚举对象</typeparam>
  122. /// <returns>Dictionary:枚举值-描述</returns>
  123. public static Dictionary<int, string> GetEnumValues<T>()
  124. {
  125. Dictionary<int, string> dictionary = new Dictionary<int, string>();
  126. foreach (var code in System.Enum.GetValues(typeof(T)))
  127. {
  128. ////获取名称
  129. //string strName = System.Enum.GetName(typeof(T), code);
  130. object[] objAttrs = code.GetType().GetField(code.ToString()).GetCustomAttributes(typeof(TextAttribute), true);
  131. if (objAttrs.Length > 0)
  132. {
  133. TextAttribute descAttr = objAttrs[0] as TextAttribute;
  134. if (!dictionary.ContainsKey((int)code))
  135. {
  136. if (descAttr != null)
  137. dictionary.Add((int)code, descAttr.Value);
  138. }
  139. //Console.WriteLine(string.Format("[{0}]", descAttr.Value));
  140. }
  141. //Console.WriteLine(string.Format("{0}={1}", code.ToString(), Convert.ToInt32(code)));
  142. }
  143. return dictionary;
  144. }
  145. }
  146. /// <summary>
  147. /// 自定义描述
  148. /// </summary>
  149. public class TextAttribute : Attribute
  150. {
  151. /// <summary>
  152. /// 描述信息
  153. /// </summary>
  154. public string Value { get; private set; }
  155. public TextAttribute(string value)
  156. {
  157. this.Value = value;
  158. }
  159. }
  160. /// <summary>
  161. /// 彩种表名称特性
  162. /// </summary>
  163. [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
  164. public sealed class TableNameAttribute : Attribute
  165. {
  166. public string TableName { get; }
  167. /// <summary>
  168. /// 构造函数
  169. /// </summary>
  170. /// <param name="tableName">表名称</param>
  171. public TableNameAttribute(string tableName)
  172. {
  173. this.TableName = tableName;
  174. }
  175. }
  176. /// <summary>
  177. /// 彩种编码特性
  178. /// </summary>
  179. [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
  180. public sealed class LotteryCIdAttribute : Attribute
  181. {
  182. public int CId { get; }
  183. /// <summary>
  184. /// 构造函数
  185. /// </summary>
  186. /// <param name="cid">CId</param>
  187. public LotteryCIdAttribute(int cid)
  188. {
  189. this.CId = cid;
  190. }
  191. }
  192. }