EnumHelper.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using SCC.Models;
  7. namespace SCC.Common
  8. {
  9. /// <summary>
  10. /// 枚举帮助类
  11. /// </summary>
  12. public static class EnumHelper
  13. {
  14. /// <summary>
  15. /// 获取彩种对应数据库表名称
  16. /// </summary>
  17. /// <param name="value">彩种枚举类型</param>
  18. /// <returns></returns>
  19. public static string GetSCCLotteryTableName(Enum value)
  20. {
  21. if (value == null)
  22. {
  23. throw new ArgumentException("value");
  24. }
  25. string tableName = value.ToString();
  26. var fieldInfo = value.GetType().GetField(tableName);
  27. var attributes = (TableNameAttribute[])fieldInfo.GetCustomAttributes(typeof(TableNameAttribute), false);
  28. if (attributes != null && attributes.Length > 0)
  29. {
  30. tableName = attributes[0].TableName;
  31. }
  32. return tableName;
  33. }
  34. /// <summary>
  35. /// 获取彩种对应编码
  36. /// </summary>
  37. /// <param name="value">彩种枚举类型</param>
  38. /// <returns></returns>
  39. public static string GetLotteryCode(Enum value)
  40. {
  41. if (value == null)
  42. {
  43. throw new ArgumentException("value");
  44. }
  45. string code = value.ToString();
  46. var fieldInfo = value.GetType().GetField(code);
  47. var attributes = (LotteryCodeAttribute[])fieldInfo.GetCustomAttributes(typeof(LotteryCodeAttribute), false);
  48. if (attributes != null && attributes.Length > 0)
  49. {
  50. code = attributes[0].Code;
  51. }
  52. return code;
  53. }
  54. /// <summary>
  55. /// 获取枚举值描述信息
  56. /// </summary>
  57. /// <param name="value"></param>
  58. /// <returns></returns>
  59. public static string GetEnumDescription(this Enum value)
  60. {
  61. if (value == null)
  62. {
  63. throw new ArgumentException("value");
  64. }
  65. string code = value.ToString();
  66. var fieldInfo = value.GetType().GetField(code);
  67. var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  68. if (attributes.Length > 0)
  69. {
  70. code = attributes[0].Description;
  71. }
  72. return code;
  73. }
  74. }
  75. }