EnumHelper.cs 2.4 KB

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