EnumHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //----------------------------------------------------------------
  2. //Module Name: EnumHelper
  3. //Purpose:
  4. //CopyRight: Muchinfo
  5. //History:
  6. //----------------------------------------------------------------
  7. //DateTime 2015/1/9 9:48:18
  8. //Author ouyang.hongbin
  9. //Description Create
  10. //----------------------------------------------------------------
  11. using Muchinfo.MTPClient.Resources;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Reflection;
  16. namespace Muchinfo.MTPClient.Data.Helper
  17. {
  18. /// <summary>
  19. /// DropDataTemplate,Enum转List Model
  20. /// </summary>
  21. public class DropDataTemplate
  22. {
  23. private int _Index;
  24. /// <summary>
  25. /// 排序
  26. /// </summary>
  27. public int Index
  28. {
  29. get { return _Index; }
  30. set { _Index = value; }
  31. }
  32. private string _Description;
  33. /// <summary>
  34. /// 说明
  35. /// </summary>
  36. public string Description
  37. {
  38. get { return _Description; }
  39. set { _Description = value; }
  40. }
  41. private int _Value;
  42. /// <summary>
  43. /// 值
  44. /// </summary>
  45. public int Value
  46. {
  47. get { return _Value; }
  48. set { _Value = value; }
  49. }
  50. private string _GroupKey;
  51. /// <summary>
  52. /// 分组关键字
  53. /// </summary>
  54. public string GroupKey
  55. {
  56. get { return _GroupKey; }
  57. set { _GroupKey = value; }
  58. }
  59. private object _Tag;
  60. /// <summary>
  61. /// 原先目标
  62. /// </summary>
  63. public object Tag
  64. {
  65. get { return _Tag; }
  66. set { _Tag = value; }
  67. }
  68. }
  69. [AttributeUsage(AttributeTargets.Field)]
  70. public class KindDiscAttribute : Attribute
  71. {
  72. private string _Description;
  73. /// <summary>
  74. /// Description
  75. /// </summary>
  76. public string Description
  77. {
  78. get { return _Description; }
  79. set { _Description = value; }
  80. }
  81. private int _Kind;
  82. /// <summary>
  83. /// kind
  84. /// </summary>
  85. public int Kind
  86. {
  87. get { return _Kind; }
  88. set
  89. {
  90. _Kind = Kind;
  91. }
  92. }
  93. /// <summary>
  94. /// ItemDiscAttribute,kind所属分类
  95. /// </summary>
  96. /// <param name="description">说明</param>
  97. /// <param name="kind">所属分类</param>
  98. public KindDiscAttribute(string description, int kind)
  99. {
  100. _Description = description;
  101. _Kind = kind;
  102. }
  103. /// <summary>
  104. /// ItemDiscAttribute
  105. /// </summary>
  106. /// <param name="Description"></param>
  107. public KindDiscAttribute(string description)
  108. {
  109. _Description = description;
  110. }
  111. }
  112. [AttributeUsage(AttributeTargets.Field)]
  113. public class ItemDiscAttribute : Attribute
  114. {
  115. private string _Description;
  116. /// <summary>
  117. /// Description
  118. /// </summary>
  119. public string Description
  120. {
  121. get { return _Description; }
  122. set { _Description = Description; }
  123. }
  124. private readonly string _Group;
  125. /// <summary>
  126. /// getGroup
  127. /// </summary>
  128. public string[] Group
  129. {
  130. get { return _Group.Split(','); }
  131. }
  132. /// <summary>
  133. /// ItemDiscAttribute,带分组的
  134. /// </summary>
  135. /// <param name="description">说明</param>
  136. /// <param name="Group">分组,如一个Enum同时具有多个分组,请用“,”号隔开</param>
  137. public ItemDiscAttribute(string description, string group)
  138. {
  139. _Description = description;
  140. _Group = group;
  141. }
  142. /// <summary>
  143. /// ItemDiscAttribute
  144. /// </summary>
  145. /// <param name="Description"></param>
  146. public ItemDiscAttribute(string Description)
  147. {
  148. _Description = Description;
  149. }
  150. }
  151. /// <summary>
  152. /// EnumHelper,枚举工具类
  153. /// </summary>
  154. public static class EnumHelper
  155. {
  156. private static Dictionary<Enum, string> dictDiscs = new Dictionary<Enum, string>();
  157. private static Dictionary<string, List<DropDataTemplate>> _EnumList = new Dictionary<string, List<DropDataTemplate>>(); //枚举缓存池
  158. /// <summary>
  159. /// 获取枚举说明,
  160. /// Sim=》TradeDirection.Register.Discription();
  161. /// </summary>
  162. /// <param name="myEnum">My enum.</param>
  163. /// <param name="enumCode">数据库枚举实际定义的代码</param>
  164. /// <returns>System.String.</returns>
  165. public static string Discription(this Enum myEnum, string enumCode="")
  166. {
  167. string strDisc = string.Empty;
  168. //if (dictDiscs.ContainsKey(myEnum))
  169. //{
  170. // strDisc = dictDiscs[myEnum];
  171. //}
  172. //else
  173. //{
  174. strDisc = GetDiscription(myEnum, enumCode);
  175. // dictDiscs.Add(myEnum, strDisc);
  176. // }
  177. return strDisc;
  178. }
  179. /// <summary>
  180. /// Gets the discription.
  181. /// </summary>
  182. /// <param name="myEnum">My enum.</param>
  183. /// <param name="enumCode">数据库枚举实际定义的代码</param>
  184. /// <returns>System.String.</returns>
  185. private static string GetDiscription(Enum myEnum, string enumCode = "")
  186. {
  187. var enumType = myEnum.GetType();
  188. var val = (int)Enum.Parse(enumType, myEnum.ToString());
  189. //枚举代码不为空,则根据枚举代码+值从常量缓存获取, 否则根据枚举名称+值从常量数据取
  190. var resxKey = string.Format("{0}_{1}", string.IsNullOrWhiteSpace(enumCode) ? enumType.Name: enumCode, val);
  191. if (ConstResxManager.ConstResx != null &&
  192. ConstResxManager.ConstResx.ResourceHashtable != null &&
  193. ConstResxManager.ConstResx.ResourceHashtable.ContainsKey(resxKey))
  194. {
  195. return ConstResxManager.ConstResx.ResourceHashtable[resxKey] as string;
  196. }
  197. FieldInfo fieldInfo = enumType.GetField(myEnum.ToString());
  198. if (fieldInfo == null)
  199. {
  200. return myEnum.ToString();
  201. }
  202. //获取字典的ItemDisc属性
  203. object[] attrs = fieldInfo.GetCustomAttributes(typeof(ItemDiscAttribute), true);
  204. ItemDiscAttribute desc = null;
  205. if (attrs != null && attrs.Length > 0)
  206. {
  207. desc = attrs[0] as ItemDiscAttribute;
  208. }
  209. //若ItemDisc为空,则根据枚举的字符从资源里查找对应描述
  210. string temp = Client_Resource.ResourceManager.GetString(desc == null ? myEnum.ToString() : desc.Description, Client_Resource.Culture);
  211. if (!string.IsNullOrWhiteSpace(temp)) return temp;
  212. return myEnum.ToString();
  213. }
  214. /// <summary>
  215. ///显示 枚举内容
  216. /// </summary>
  217. /// <typeparam name="T">属性类型</typeparam>
  218. /// <param name="myEnum"></param>
  219. /// <returns></returns>
  220. public static string Discription<T>(this Enum myEnum)
  221. {
  222. FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
  223. object[] attrs = fieldInfo.GetCustomAttributes(typeof(T), true);
  224. if (attrs != null && attrs.Length > 0)
  225. {
  226. var attributesType = attrs[0].GetType();
  227. var property = attributesType.GetProperty("Description");
  228. return (string)property.GetValue(attrs[0], null);
  229. }
  230. return myEnum.ToString();
  231. }
  232. public static int GetKind(Enum myEnum)
  233. {
  234. FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
  235. if (fieldInfo != null)
  236. {
  237. object[] attrs = fieldInfo.GetCustomAttributes(typeof(KindDiscAttribute), true);
  238. if (attrs != null && attrs.Length > 0)
  239. {
  240. KindDiscAttribute kind = attrs[0] as KindDiscAttribute;
  241. if (kind != null)
  242. {
  243. return kind.Kind;
  244. }
  245. }
  246. }
  247. return 0;
  248. }
  249. /// <summary>
  250. /// 将枚举转换成 List DropDataTemplate
  251. /// </summary>
  252. /// <param name="enumType">枚举类型</param>
  253. /// <returns></returns>
  254. public static List<DropDataTemplate> EnumToDictionary(Type enumType)
  255. {
  256. string keyName = enumType.FullName;
  257. if (!_EnumList.ContainsKey(keyName))
  258. {
  259. var list = new List<DropDataTemplate>();
  260. int index = 0;
  261. foreach (int i in Enum.GetValues(enumType))
  262. {
  263. string name = Enum.GetName(enumType, i);
  264. //取显示名称
  265. string description = string.Empty;
  266. object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
  267. if (atts.Length > 0)
  268. {
  269. description = ((ItemDiscAttribute)atts[0]).Description;
  270. }
  271. list.Add(new DropDataTemplate()
  272. {
  273. Index = index++,
  274. Value = i,
  275. Description = string.IsNullOrEmpty(description) ? name : description,
  276. Tag = Enum.Parse(enumType, name),
  277. });
  278. }
  279. object syncObj = new object();
  280. if (!_EnumList.ContainsKey(keyName))
  281. {
  282. lock (syncObj)
  283. {
  284. if (!_EnumList.ContainsKey(keyName))
  285. {
  286. _EnumList.Add(keyName, list);
  287. }
  288. }
  289. }
  290. }
  291. return _EnumList[keyName];
  292. }
  293. /// <summary>
  294. /// 将枚举转换成 List DropDataTemplate
  295. /// </summary>
  296. /// <param name="enumType">枚举类型</param>
  297. /// <param name="groupName">分组名称</param>
  298. /// <returns></returns>
  299. public static List<DropDataTemplate> EnumToDictionary(Type enumType, string groupName)
  300. {
  301. string keyName = enumType.FullName;
  302. if (!_EnumList.ContainsKey(keyName))
  303. {
  304. var list = new List<DropDataTemplate>();
  305. int index = 0;
  306. foreach (int i in Enum.GetValues(enumType))
  307. {
  308. string name = Enum.GetName(enumType, i);
  309. //取显示名称
  310. string description = string.Empty;
  311. string group = string.Empty;
  312. object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
  313. if (!string.IsNullOrWhiteSpace(groupName) && ((ItemDiscAttribute)atts[0]).Group.Where(x => x == groupName).Count() > 0)
  314. {
  315. if (atts.Length > 0)
  316. {
  317. description = ((ItemDiscAttribute)atts[0]).Description;
  318. }
  319. list.Add(new DropDataTemplate()
  320. {
  321. Index = index++,
  322. Value = i,
  323. GroupKey = groupName,
  324. Description = string.IsNullOrEmpty(description) ? name : description,
  325. Tag = Enum.Parse(enumType, name),
  326. });
  327. }
  328. }
  329. object syncObj = new object();
  330. if (!_EnumList.ContainsKey(keyName))
  331. {
  332. lock (syncObj)
  333. {
  334. if (!_EnumList.ContainsKey(keyName))
  335. {
  336. _EnumList.Add(keyName, list);
  337. }
  338. }
  339. }
  340. }
  341. return _EnumList[keyName];
  342. }
  343. /// <summary>
  344. /// 将枚举转换成 Dictionary<object,string>
  345. /// </summary>
  346. /// <param name="enumType">枚举类型</param>
  347. /// <returns></returns>
  348. public static Dictionary<object, string> ToDictionaryEnum(Type enumType)
  349. {
  350. string keyName = enumType.FullName;
  351. var list = new Dictionary<object, string>();
  352. // int index = 0;
  353. foreach (int i in Enum.GetValues(enumType))
  354. {
  355. string name = Enum.GetName(enumType, i);
  356. //取显示名称
  357. string description = string.Empty;
  358. object[] atts = enumType.GetField(name).GetCustomAttributes(typeof(ItemDiscAttribute), false);
  359. if (atts.Length > 0)
  360. {
  361. description = ((ItemDiscAttribute)atts[0]).Description;
  362. }
  363. list[Enum.Parse(enumType, name)] = string.IsNullOrEmpty(description) ? name : description;
  364. }
  365. return list;
  366. }
  367. /// <summary>
  368. /// 获取枚举值对应的显示名称
  369. /// </summary>
  370. /// <param name="enumType">枚举类型</param>
  371. /// <param name="intValue">枚举项对应的int值</param>
  372. /// <returns></returns>
  373. public static
  374. string GetEnumShowName(Type enumType, int Index)
  375. {
  376. return EnumToDictionary(enumType)[Index].Description;
  377. }
  378. }
  379. }