|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- ```
- <PackageReference Include="Ardalis.SmartEnum" Version="2.0.0" />
- ```
-
-
-
- ```
- /// <summary>
- /// 枚举作为状态机
- /// </summary>
- public abstract class ReservationStatus : SmartEnum<ReservationStatus>
- {
- private ReservationStatus(string name, int value) : base(name, value) { }
-
- public abstract bool CanTransitionTo(ReservationStatus next);
-
- public static readonly ReservationStatus New = new NewStatus();
- public static readonly ReservationStatus Accepted = new AcceptedStatus();
- public static readonly ReservationStatus Paid = new PaidStatus();
- public static readonly ReservationStatus Cancelled = new CancelledStatus();
-
- private sealed class NewStatus : ReservationStatus
- {
- public NewStatus() : base("New", 0)
- {
- }
-
- public override bool CanTransitionTo(ReservationStatus next) =>
- next == ReservationStatus.Accepted || next == ReservationStatus.Cancelled;
- }
-
- private sealed class AcceptedStatus : ReservationStatus
- {
- public AcceptedStatus() : base("Accepted", 1)
- {
- }
-
- public override bool CanTransitionTo(ReservationStatus next) =>
- next == ReservationStatus.Paid || next == ReservationStatus.Cancelled;
- }
-
- private sealed class PaidStatus : ReservationStatus
- {
- public PaidStatus() : base("Paid", 2)
- {
- }
-
- public override bool CanTransitionTo(ReservationStatus next) =>
- next == ReservationStatus.Cancelled;
- }
-
- private sealed class CancelledStatus : ReservationStatus
- {
- public CancelledStatus() : base("Cancelled", 3)
- {
- }
-
- public override bool CanTransitionTo(ReservationStatus next) =>
- false;
- }
-
-
- }
-
- /// <summary>
- /// 枚举可以初始化
- /// </summary>
- public sealed class TestEnum : SmartEnum<TestEnum>
- {
- private TestEnum(string name, int value) : base(name, value)
- {
-
- }
-
- public static readonly TestEnum One = new TestEnum(nameof(One), 1);
- public static readonly TestEnum Two = new TestEnum(nameof(Two), 2);
- public static readonly TestEnum Three = new TestEnum(nameof(Three), 3);
- }
-
- /// <summary>
- /// 别名
- /// </summary>
- public sealed class TestEnum1 : SmartEnum<TestEnum1>
- {
- //真实的值通过构造函数来设定
- private TestEnum1(string name, ushort value) : base(name, value) { }
-
- /// <summary>
- /// One看作是别名
- /// </summary>
- public static readonly TestEnum1 One = new TestEnum1("a", 1);
- public static readonly TestEnum1 Two = new TestEnum1("b", 2);
- public static readonly TestEnum1 Three = new TestEnum1("a", 3);
- }
-
- /// <summary>
- /// 枚举也可以被继承
- /// 也可以是抽象
- /// </summary>
- public abstract class EmployeeType : SmartEnum<EmployeeType>
- {
- private EmployeeType(string name, int value):base(name, value) { }
- public abstract decimal BonusSize { get; }
-
- public static readonly EmployeeType Manager = new ManagerType();
- public static readonly EmployeeType Assistant = new AssistantType();
-
-
- private sealed class ManagerType : EmployeeType
- {
- public ManagerType():base("Manager",1)
- {
-
- }
-
- public override decimal BonusSize => 10m;
- }
-
- private sealed class AssistantType : EmployeeType
- {
- public AssistantType() : base("Assistant",2)
- {
-
- }
-
- public override decimal BonusSize => 20m;
- }
- }
- ```
-
|