我有一个包含常量数据集合的类型。常量由在我的程序之外定义的标准定义。类型如下所示:
public class IAmImmutable
{
public IAmImmutable(string member1, string member2)
{
this.Member1 = member1;
this.Member2 = member2;
}
public string Member1 { get; private set; }
public string Member2 { get; private set; }
public static readonly Instance1 = new IAmImmutable("abc", "def");
public static readonly Instance2 = new IAmImmutable("example", "data");
public static readonly Instance3 = new IAmImmutable("for", "stackoverflow");
public static readonly Instance4 = new IAmImmutable("these are", "constant fields");
public static readonly Instance5 = new IAmImmutable("42", "1729");
/* ... */
public static readonly Instance1000 = new IAmImmutable("HUNGRY EVIL", "ZOMBIES");
}
这导致CA2104:DoNotDeclareReadOnlyMutableReferenceTypes
来自 FxCop 的数千次检测。此检测中的注释表明,如果指示的类型是不可变的,则应该抑制检测,在这种情况下就是这样。但是,如果我可以避免这样做,我不希望有成千上万的压制。
是否可以将此类型标记为不可变,从而防止发生这种检测?