简体   繁体   English

CA1008:枚举应具有零值和标志枚举

[英]CA1008: Enums should have zero value and Flag enums

even when I'm completely aware of why the CA1008 warning exists I don't know how to avoid it in the following situation. 即使我完全知道为什么存在CA1008警告,在以下情况下我也不知道如何避免它。 I have a Flag enum with the following meanings: 我有一个Flag枚举,含义如下:

ValidValue = 0x01
WrittenValue = 0x02

So in this case 0 means InvalidValueNonWritten instead of None. 因此,在这种情况下,0表示InvalidValueNonWritten而不是None。 The rule says 规则

Do not suppress a warning from this rule except for flags-attributed enumerations that have previously shipped. 除先前附带的带有标志属性的枚举外,请勿禁止此规则发出警告。

In this case I've not shipped the enum so how can I prevent this warning? 在这种情况下,我还没有交付枚举,那么如何防止此警告?

EDIT: 编辑:

The warning explicitily says: 该警告明确指出:

Warning 86 CA1008 : Microsoft.Design : In enum 'XXX', change the name of 'XXX.InvalidValueNonWritten' to 'None'. 警告86 CA1008:Microsoft.Design:在枚举“ XXX”中,将“ XXX.InvalidValueNonWritten”的名称更改为“ None”。

EDIT 2: 编辑2:

More states of the enum: 枚举的更多状态:

CommandValue = 0x04 // Otherwise it is DataValue
InmediateValue = 0x08 // Otherwise it is Deferred

At this particular moment, the 0 value has a "not valid or written" meaning. 在此特定时刻,0值具有“无效或已写入”的含义。 However, this will no longer be the case if you add more enum members. 但是,如果添加更多枚举成员,则不再是这种情况。 For example, if you add ApprovedValue = 0x04 , 0 will start to mean "not valid or written or approved". 例如,如果添加ApprovedValue = 0x04 ,则0表示“无效,书面或批准”。 This is the main reason for always using a None name for the 0 value. 这是始终将None名称用作0值的主要原因。

If None doesn't make sense as a name, this usually signals a flaw in the enum design or name. 如果“ None作为名称没有意义,则通常表示该枚举设计或名称存在缺陷。 In your case, it sounds like the enum actually represents steps that the value has passed through, as opposed to state of the value (for which a flags enum wouldn't typically be used). 在您的情况下,听起来好像枚举实际上表示值已通过的步骤 ,而不是数值的状态 (通常不会使用标志枚举)。 Might the following be a closer representation of what you intend (where an associated class might have a property named something like CompletedValueProcessingSteps )? 以下内容可能更接近您的期望(关联的类可能具有一个名为CompletedValueProcessingSteps类的属性)?

[Flags]
enum ValueProcessingSteps
{
    None = 0,
    Validation = 1,
    Writing = 2
}

If 0 means InvalidValueNonWritten then add InvalidValueNonWritten into the enum. 如果为0表示InvalidValueNonWritten,则将InvalidValueNonWritten添加到枚举中。 Then this warning won't happen. 这样就不会发生此警告。 If you really shouldn't have 0 in your enum suppress the warning in code and ensure you put the justification in for future reference. 如果您的枚举中确实不应该包含0,请在代码中取消警告,并确保将理由放在后面以供将来参考。

If you are expecting an exception due to 0 not been valid it is better to perform the validation in your method using the enum. 如果您期望由于0无效而导致的异常无效,则最好使用枚举在您的方法中执行验证。 Also this is better for serialization, for example if you are using serialization on models the model will de-serialize with a missing member from the source. 同样,这对于序列化比较好,例如,如果您在模型上使用序列化,则模型将使用源中缺少的成员反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM