EnumHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Models.Entity.LottomatBaseDB;
  6. using Models.Entity.LotteryNumDB;
  7. using Models;
  8. using System.Reflection;
  9. namespace Common
  10. {
  11. /// <summary>
  12. /// 枚举帮助类
  13. /// </summary>
  14. public static class EnumHelper
  15. {
  16. /// <summary>
  17. /// 获取彩种对应数据库表名称
  18. /// </summary>
  19. /// <param name="value">彩种枚举类型</param>
  20. /// <returns></returns>
  21. public static string GetSCCLotteryTableName(Enum value)
  22. {
  23. if (value == null)
  24. {
  25. throw new ArgumentException("value");
  26. }
  27. string TableName = value.ToString();
  28. var fieldInfo = value.GetType().GetField(TableName);
  29. var attributes = (TableNameAttribute[])fieldInfo.GetCustomAttributes(typeof(TableNameAttribute), false);
  30. if (attributes != null && attributes.Length > 0)
  31. {
  32. TableName = attributes[0].TableName;
  33. }
  34. return TableName;
  35. }
  36. /// <summary>
  37. /// 获取彩种对应TDK标签
  38. ///
  39. /// </summary>
  40. /// <param name="value">彩种枚举类型</param>
  41. /// <returns></returns>
  42. public static string GetSCCLotteryTDKTypeName(Enum value)
  43. {
  44. if (value == null)
  45. {
  46. throw new ArgumentException("value");
  47. }
  48. string TableName = value.ToString();
  49. var fieldInfo = value.GetType().GetField(TableName);
  50. var attributes = (TDKTypeAttribute[])fieldInfo.GetCustomAttributes(typeof(TDKTypeAttribute), false);
  51. if (attributes != null && attributes.Length > 0)
  52. {
  53. TableName = attributes[0].TDKType;
  54. }
  55. return TableName;
  56. }
  57. /// <summary>
  58. /// 获取彩种对应编码
  59. /// </summary>
  60. /// <param name="value">彩种枚举类型</param>
  61. /// <returns></returns>
  62. public static string GetLotteryCode(Enum value)
  63. {
  64. if (value == null)
  65. {
  66. throw new ArgumentException("value");
  67. }
  68. string code = value.ToString();
  69. var fieldInfo = value.GetType().GetField(code);
  70. var attributes = (LotteryCodeAttribute[])fieldInfo.GetCustomAttributes(typeof(LotteryCodeAttribute), false);
  71. if (attributes != null && attributes.Length > 0)
  72. {
  73. code = attributes[0].Code;
  74. }
  75. return code;
  76. }
  77. /// <summary>
  78. /// 获取彩种对应编码
  79. /// </summary>
  80. /// <param name="value">彩种枚举类型</param>
  81. /// <returns></returns>
  82. public static string GetLotteryText(Enum value)
  83. {
  84. if (value == null)
  85. {
  86. throw new ArgumentException("value");
  87. }
  88. string code = value.ToString();
  89. var fieldInfo = value.GetType().GetField(code);
  90. var attributes = (TextAttribute[])fieldInfo.GetCustomAttributes(typeof(TextAttribute), false);
  91. if (attributes != null && attributes.Length > 0)
  92. {
  93. code = attributes[0].Text;
  94. }
  95. return code;
  96. }
  97. /// <summary>
  98. /// 获取咨询数据库的表名
  99. /// </summary>
  100. /// <param name="value"></param>
  101. /// <returns></returns>
  102. public static string GetZXTableName<T>()
  103. {
  104. var type = typeof(T)
  105. ; var attribute = type.GetCustomAttributes(typeof(ZXTableNameAttribute), false).FirstOrDefault();
  106. if (attribute == null)
  107. {
  108. return null;
  109. }
  110. return ((ZXTableNameAttribute)attribute).TableName;
  111. }
  112. /// <summary>
  113. /// 获取备注信息对应的枚举值
  114. /// </summary>
  115. /// <param name="text"></param>
  116. /// <returns></returns>
  117. public static string GETEnumTypeByText<T>( string text)
  118. {
  119. Type type = typeof(T);
  120. FieldInfo[] fds = type.GetFields();
  121. foreach (var fd in fds)
  122. {
  123. object[] attrs = fd.GetCustomAttributes(typeof(TextAttribute), false);
  124. foreach (TextAttribute attr in attrs)
  125. {
  126. var name = attr.Text;
  127. if (name == text)
  128. return fd.Name;
  129. }
  130. }
  131. return null;
  132. }
  133. }
  134. }