鼎鼎知识库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

6、强类型枚举.md 3.7KB

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ```
  2. <PackageReference Include="Ardalis.SmartEnum" Version="2.0.0" />
  3. ```
  4. ```
  5. /// <summary>
  6. /// 枚举作为状态机
  7. /// </summary>
  8. public abstract class ReservationStatus : SmartEnum<ReservationStatus>
  9. {
  10. private ReservationStatus(string name, int value) : base(name, value) { }
  11. public abstract bool CanTransitionTo(ReservationStatus next);
  12. public static readonly ReservationStatus New = new NewStatus();
  13. public static readonly ReservationStatus Accepted = new AcceptedStatus();
  14. public static readonly ReservationStatus Paid = new PaidStatus();
  15. public static readonly ReservationStatus Cancelled = new CancelledStatus();
  16. private sealed class NewStatus : ReservationStatus
  17. {
  18. public NewStatus() : base("New", 0)
  19. {
  20. }
  21. public override bool CanTransitionTo(ReservationStatus next) =>
  22. next == ReservationStatus.Accepted || next == ReservationStatus.Cancelled;
  23. }
  24. private sealed class AcceptedStatus : ReservationStatus
  25. {
  26. public AcceptedStatus() : base("Accepted", 1)
  27. {
  28. }
  29. public override bool CanTransitionTo(ReservationStatus next) =>
  30. next == ReservationStatus.Paid || next == ReservationStatus.Cancelled;
  31. }
  32. private sealed class PaidStatus : ReservationStatus
  33. {
  34. public PaidStatus() : base("Paid", 2)
  35. {
  36. }
  37. public override bool CanTransitionTo(ReservationStatus next) =>
  38. next == ReservationStatus.Cancelled;
  39. }
  40. private sealed class CancelledStatus : ReservationStatus
  41. {
  42. public CancelledStatus() : base("Cancelled", 3)
  43. {
  44. }
  45. public override bool CanTransitionTo(ReservationStatus next) =>
  46. false;
  47. }
  48. }
  49. /// <summary>
  50. /// 枚举可以初始化
  51. /// </summary>
  52. public sealed class TestEnum : SmartEnum<TestEnum>
  53. {
  54. private TestEnum(string name, int value) : base(name, value)
  55. {
  56. }
  57. public static readonly TestEnum One = new TestEnum(nameof(One), 1);
  58. public static readonly TestEnum Two = new TestEnum(nameof(Two), 2);
  59. public static readonly TestEnum Three = new TestEnum(nameof(Three), 3);
  60. }
  61. /// <summary>
  62. /// 别名
  63. /// </summary>
  64. public sealed class TestEnum1 : SmartEnum<TestEnum1>
  65. {
  66. //真实的值通过构造函数来设定
  67. private TestEnum1(string name, ushort value) : base(name, value) { }
  68. /// <summary>
  69. /// One看作是别名
  70. /// </summary>
  71. public static readonly TestEnum1 One = new TestEnum1("a", 1);
  72. public static readonly TestEnum1 Two = new TestEnum1("b", 2);
  73. public static readonly TestEnum1 Three = new TestEnum1("a", 3);
  74. }
  75. /// <summary>
  76. /// 枚举也可以被继承
  77. /// 也可以是抽象
  78. /// </summary>
  79. public abstract class EmployeeType : SmartEnum<EmployeeType>
  80. {
  81. private EmployeeType(string name, int value):base(name, value) { }
  82. public abstract decimal BonusSize { get; }
  83. public static readonly EmployeeType Manager = new ManagerType();
  84. public static readonly EmployeeType Assistant = new AssistantType();
  85. private sealed class ManagerType : EmployeeType
  86. {
  87. public ManagerType():base("Manager",1)
  88. {
  89. }
  90. public override decimal BonusSize => 10m;
  91. }
  92. private sealed class AssistantType : EmployeeType
  93. {
  94. public AssistantType() : base("Assistant",2)
  95. {
  96. }
  97. public override decimal BonusSize => 20m;
  98. }
  99. }
  100. ```