| 属性 | 值 |
|---|---|
| 规则 ID | CA2227 |
| 标题 | 集合属性应为只读 |
| 类别 | 使用情况 |
| 修复会引起中断还是不会引起中断 | 突发 |
| 在 .NET 10 中默认启用 | 否 |
| 适用的语言 | C# 和 Visual Basic |
原因
外部可见的可写属性属于实现 System.Collections.ICollection 的类型。 此规则将忽略数组、索引器(名称为“Item”的属性)、不可变集合、只读集合和权限集。
规则说明
可写集合属性允许用户将集合替换为完全不同的集合。 只读或 init-only 属性可防止替换集合,但仍允许设置各个成员。 如果替换集合是一个目标,首选的设计模式是包含一个从集合中删除所有元素的方法,以及重新填充集合的方法。 有关此情况的示例,请参阅 Clear 类的 AddRange 和 System.Collections.ArrayList 方法。
二进制和 XML 序列化都支持作为集合的只读属性。 该 System.Xml.Serialization.XmlSerializer 类具有实现 ICollection 和 System.Collections.IEnumerable 可序列化的类型的特定要求。
如何解决违规
使用以下方法之一来修复对这一规则的违规行为:
将属性设为只读或仅限于初始化。 只读或 init-only 属性可防止替换集合,同时仍允许设置单个成员。 如果设计需要替换集合的内容,请添加方法来清除和重新填充集合。 有关此模式的示例,请参阅ArrayList.Clear和ArrayList.AddRange方法。
将属性类型更改为只读集合类型。 如果调用方不需要修改集合,请将属性类型更改为只读集合,例如 ReadOnlyCollection<T>。 这种方法在类型签名中明确表达了只读意图。
将属性类型更改为线程安全的并发集合类型,同时使属性保持只读。 如果设计需要多个线程同时修改集合,请公开一个只读属性(无 setter),其类型为并发集合,例如 ConcurrentBag<T>。 CA2227 由可写集合属性触发,而不是由集合类型触发,因此该属性仍必须为只读。 并发集合选项仅处理返回的集合实例的线程安全突变。
何时禁止显示警告
如果属性是数据传输对象 (DTO) 类的一部分,则可禁止显示警告。
否则,请勿禁止显示此规则的警告。
抑制警告
如果只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用该规则。
#pragma warning disable CA2227
// The code that's violating the rule is on this line.
#pragma warning restore CA2227
若要对文件、文件夹或项目禁用该规则,请在none中将其严重性设置为 。
[*.{cs,vb}]
dotnet_diagnostic.CA2227.severity = none
有关详细信息,请参阅如何禁止显示代码分析警告。
示例
以下示例演示具有可写集合属性的类型,以及如何直接替换集合。 它还展示了使用Clear和AddRange方法替换只读集合属性的首选方式。
public class WritableCollection
{
public ArrayList SomeStrings
{
get;
// This set accessor violates rule CA2227.
// To fix the code, remove this set accessor or change it to init.
set;
}
public WritableCollection()
{
SomeStrings = new ArrayList(new string[] { "one", "two", "three" });
}
}
class ReplaceWritableCollection
{
static void Main2227()
{
ArrayList newCollection = ["a", "new", "collection"];
WritableCollection collection = new()
{
// This line of code demonstrates how the entire collection
// can be replaced by a property that's not read only.
SomeStrings = newCollection
};
// If the intent is to replace an entire collection,
// implement and/or use the Clear() and AddRange() methods instead.
collection.SomeStrings.Clear();
collection.SomeStrings.AddRange(newCollection);
}
}
Public Class WritableCollection
' This property violates rule CA2227.
' To fix the code, add the ReadOnly modifier to the property:
' ReadOnly Property SomeStrings As ArrayList
Property SomeStrings As ArrayList
Sub New()
SomeStrings = New ArrayList(New String() {"one", "two", "three"})
End Sub
End Class
Class ViolatingVersusPreferred
Shared Sub Main2227()
Dim newCollection As New ArrayList(New String() {"a", "new", "collection"})
Dim collection As New WritableCollection()
' This line of code demonstrates how the entire collection
' can be replaced by a property that's not read only.
collection.SomeStrings = newCollection
' If the intent is to replace an entire collection,
' implement and/or use the Clear() and AddRange() methods instead.
collection.SomeStrings.Clear()
collection.SomeStrings.AddRange(newCollection)
End Sub
End Class