CA2227:Collection プロパティは読み取り専用でなければなりません

プロパティ
ルール ID CA2227
Title Collection プロパティは読み取り専用でなければなりません
[カテゴリ] 使用方法
修正が中断ありか中断なしか あり
.NET 8 では既定で有効 いいえ

原因

外部から参照できる、書き込み可能なプロパティの型は System.Collections.ICollection を実装します。 この規則は、配列、インデクサー (名前が 'Item' のプロパティ)、変更できないコレクション、読み取り専用コレクション、およびアクセス許可セットを無視します。

規則の説明

書き込み可能なコレクション プロパティにより、ユーザーはコレクションを完全に異なるコレクションで置換できます。 読み取り専用または初期化専用プロパティでは、コレクションの置き換えが停止されますが、個々のメンバーの設定は引き続き許可されます。 コレクションを置き換えることが目標である場合、推奨される設計パターンは、コレクションからすべての要素を削除するメソッドと、コレクションを再作成するメソッドを含めることです。 このパターンの例については、System.Collections.ArrayList クラスの Clear および AddRange メソッドを参照してください。

バイナリ シリアル化と XML シリアル化の両方で、コレクションである読み取り専用プロパティがサポートされます。 シリアル可能であるために、System.Xml.Serialization.XmlSerializer クラスには、ICollection および System.Collections.IEnumerable を実装する型に関する特定の要件があります。

違反の修正方法

この規則の違反を修正するには、プロパティを読み取り専用または初期化専用にします。 設計で必要な場合は、コレクションをクリアして再作成するメソッドを追加します。

どのようなときに警告を抑制するか

プロパティがデータ転送オブジェクト (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 = new ArrayList(new string[] { "a", "new", "collection" });

        WritableCollection collection = 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);
    }
}
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

関連項目