EnumExtensions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. *
  3. * @date 2014-10-22
  4. *
  5. * @author 邓尹平
  6. *
  7. * @par 成员列表类 说明
  8. * 主要负责管理成员 提供搜索\排序和操作的方法
  9. *
  10. * @par 版权声明
  11. * 深圳市多元世纪信息技术有限公司 版权所有
  12. *
  13. * @see 使用此类时参照一些其他类可以写在这里
  14. *
  15. * @todo 该类有待完成的任务 一条条列出 完成一条删除一条
  16. *
  17. * @bug 该类已知的Bug一条条列出 完成一条删除一条
  18. *
  19. */
  20. using Muchinfo.MTPClient.Data.Helper;
  21. using Muchinfo.MTPClient.Resources;
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Reflection;
  26. namespace Muchinfo.MTPClient.Data.Extensions
  27. {
  28. public static class EnumExtensions
  29. {
  30. private static Dictionary<Enum, string> dictDiscs = new Dictionary<Enum, string>();
  31. private static Dictionary<string, List<DropDataTemplate>> _EnumList = new Dictionary<string, List<DropDataTemplate>>(); //枚举缓存池
  32. /// <summary>
  33. /// 获取枚举描述
  34. /// </summary>
  35. /// <param name="en">The en.</param>
  36. /// <returns>System.String.</returns>
  37. public static string ToDescription(this Enum en)
  38. {
  39. try
  40. {
  41. Type type = en.GetType();
  42. MemberInfo[] memInfo = type.GetMember(en.ToString());
  43. if (memInfo.Length > 0)
  44. {
  45. object[] attrs = memInfo[0].GetCustomAttributes(typeof(ItemDiscAttribute), false);
  46. if (attrs.Length > 0)
  47. return ((ItemDiscAttribute)attrs[0]).Description;
  48. }
  49. return en.ToString();
  50. }
  51. catch
  52. {
  53. return string.Empty;
  54. }
  55. }
  56. /// <summary>
  57. /// 获取枚举说明,
  58. /// Sim=》TradeDirection.Register.Discription();
  59. /// </summary>
  60. /// <param name="myEnum"></param>
  61. /// <returns></returns>
  62. public static string Discription1(this Enum myEnum)
  63. {
  64. string strDisc = string.Empty;
  65. if (dictDiscs.ContainsKey(myEnum))
  66. {
  67. strDisc = dictDiscs[myEnum];
  68. }
  69. else
  70. {
  71. strDisc = GetDiscription(myEnum);
  72. dictDiscs.Add(myEnum, strDisc);
  73. }
  74. return strDisc;
  75. }
  76. private static string GetDiscription(Enum myEnum)
  77. {
  78. FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
  79. if (fieldInfo == null)
  80. {
  81. return myEnum.ToString();
  82. }
  83. object[] attrs = fieldInfo.GetCustomAttributes(typeof(ItemDiscAttribute), true);
  84. if (attrs != null && attrs.Length > 0)
  85. {
  86. ItemDiscAttribute desc = attrs[0] as ItemDiscAttribute;
  87. if (desc != null)
  88. {
  89. string temp = Client_Resource.ResourceManager.GetString(desc.Description, Client_Resource.Culture);
  90. return temp;
  91. }
  92. }
  93. return myEnum.ToString();
  94. }
  95. /// <summary>
  96. /// 枚举转换成字典Key-枚举值, Value-枚举描述
  97. /// </summary>
  98. /// <typeparam name="TEnum">The type of the t enum.</typeparam>
  99. /// <returns>Dictionary{``0System.String}.</returns>
  100. public static Dictionary<TEnum, string> ToDictionary<TEnum>() where TEnum : struct
  101. {
  102. try
  103. {
  104. var result = new Dictionary<TEnum, string>();
  105. foreach (var item in Enum.GetValues(typeof(TEnum)))
  106. {
  107. var enumItem = (Enum)item;
  108. result.Add((TEnum)item, enumItem.Discription());
  109. }
  110. return result.OrderBy(z => z.Key).ToDictionary(z => z.Key, z => z.Value);
  111. }
  112. catch
  113. {
  114. return null;
  115. }
  116. }
  117. }
  118. }