我有这个 2 枚举,我需要将待处理状态与事务状态相关联
解释一下,对于每笔交易,我使用交易状态枚举只有一个状态,那么对于每个交易状态,我使用待处理状态枚举有许多待处理的原因?
/// <summary>
/// Represent all available status for Transaction
/// </summary>
public enum TransactionStatus
{
New =0,
Submitted =1,
PendingStatus = 2,
Accepted = 3,
Rejected =4,
InProgress =5,
Completed=6,
Failed=7,
Canceled=8
}
/// <summary>
/// Represent all available pending status for Transaction
/// </summary>
public enum PendingStatus
{
PendingA =0,
PendingX =1,
PendingY = 2,
}
我该怎么做?
您可以使用枚举值作为位标志,如此处所述。
通过这种方式,您可以将待处理状态和交易状态混合在一起。
/// <summary>
/// Represent all available status for Transaction
/// </summary>
[Flags]
public enum TransactionStatus
{
New = 0,
Submitted = 1,
PendingStatus = 2,
Accepted = 4,
Rejected = 8,
InProgress = 16,
Completed = 32,
Failed = 64,
Canceled = 128
}
/// <summary>
/// Represent all available pending status for Transaction
/// </summary>
[Flags]
public enum PendingStatus
{
PendingA = 256,
PendingX = 512,
PendingY = 1024
}
// Example to set transaction as accepted and pending
var MyTransactionStatus = Accepted & PendingA;
// How to check transaction is pendingA regardless of its status ?
if (MyTransactionStatus & PendingA == PendingA) ...
我会使用FlagsAttribute。因此,您可以使用二进制比较。
[FlagsAttribute]
public enum TransactionStatus
{
New = 0,
Submitted = 1,
PendingStatus = 2,
Accepted = 4,
// (...)
}
[FlagsAttribute]
public enum PendingStatus
{
PendingA = 256,
PendingX = 512,
PendingY = 1024,
}