CA1010:集合應該實作泛型介面

屬性
規則識別碼 CA1010
標題 集合應該實作泛型介面
類別 設計
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 No

原因

型別會實作 System.Collections.IEnumerable 介面,但不會實 System.Collections.Generic.IEnumerable<T> 作 介面,而包含的元件則以 .NET 為目標。 此規則會忽略實 System.Collections.IDictionary作的類型。

根據預設,此規則只會查看外部可見的類型,但這是可設定。 您也可以設定其他介面,要求實作泛型介面。

檔案描述

若要放寬集合的可用性,請實作其中一個泛型集合介面。 然後,集合可以用來填入泛型集合類型,如下所示:

如何修正違規

若要修正此規則的違規,請實作下列其中一個泛型集合介面:

隱藏警告的時機

隱藏此規則的警告是安全的;不過,使用集合會更加有限。

隱藏警告

如果您只想要隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

#pragma warning disable CA1010
// The code that's violating the rule is on this line.
#pragma warning restore CA1010

若要停用檔案、資料夾或項目的規則,請在組態檔中將其嚴重性設定為 。none

[*.{cs,vb}]
dotnet_diagnostic.CA1010.severity = none

如需詳細資訊,請參閱 如何隱藏程式代碼分析警告

設定程式代碼以分析

使用下列選項來設定程式代碼基底要執行此規則的部分。

您可以只針對此規則、它套用的所有規則,或針對套用至此類別的所有規則(設計)設定這些選項。 如需詳細資訊,請參閱 程式代碼品質規則組態選項

包含特定 API 介面

您可以根據程式代碼基底的存取範圍,設定要執行此規則的部分。 例如,若要指定規則只應該針對非公用 API 介面執行,請將下列機碼/值組新增至 專案中的 .editorconfig 檔案:

dotnet_code_quality.CAXXXX.api_surface = private, internal

其他必要的泛型介面

您可以使用其必要的泛型完整介面來設定介面名稱清單(以 |分隔 ->)。

允許的介面格式:

  • 介面名稱僅限 (包含名稱的所有介面,不論包含類型或命名空間為何)。
  • 符號 文件識別碼格式 的完整名稱,具有選擇性 T: 前置詞。

範例:

選項值 摘要
dotnet_code_quality.CA1010.additional_required_generic_interfaces = ISomething->System.Collections.Generic.IEnumerable`1 不論實作其命名空間為何,實作的所有型 ISomething 別也會實 System.Collections.Generic.IEnumerable<T>作 。
dotnet_code_quality.CA1010.additional_required_generic_interfaces = T:System.Collections.IDictionary->T:System.Collections.Generic.IDictionary`2 實作的所有型別 System.Collections.IDictionary 都預期也會實作 System.Collections.Generic.IDictionary<TKey,TValue>

範例

下列範例顯示衍生自非泛型 CollectionBase 類別並違反此規則的類別。

public class Book
{
    public Book()
    {
    }
}

public class BookCollection : CollectionBase
{
    public BookCollection()
    {
    }

    public void Add(Book value)
    {
        InnerList.Add(value);
    }

    public void Remove(Book value)
    {
        InnerList.Remove(value);
    }

    public void Insert(int index, Book value)
    {
        InnerList.Insert(index, value);
    }

    public Book? this[int index]
    {
        get { return (Book?)InnerList[index]; }
        set { InnerList[index] = value; }
    }

    public bool Contains(Book value)
    {
        return InnerList.Contains(value);
    }

    public int IndexOf(Book value)
    {
        return InnerList.IndexOf(value);
    }

    public void CopyTo(Book[] array, int arrayIndex)
    {
        InnerList.CopyTo(array, arrayIndex);
    }
}

若要修正此規則的違規,請執行下列其中一項:

依介面實作修正

下列範例會藉由實作這些泛型介面來修正違規: IEnumerable<T>ICollection<T>IList<T>

public class Book
{
    public Book()
    {
    }
}

public class BookCollection : CollectionBase, IList<Book?>
{
    public BookCollection()
    {
    }

    int IList<Book?>.IndexOf(Book? item)
    {
        return this.List.IndexOf(item);
    }

    void IList<Book?>.Insert(int location, Book? item)
    {
    }

    Book? IList<Book?>.this[int index]
    {
        get => (Book?)this.List[index];
        set { }
    }

    void ICollection<Book?>.Add(Book? item)
    {
    }

    bool ICollection<Book?>.Contains(Book? item)
    {
        return true;
    }

    void ICollection<Book?>.CopyTo(Book?[] array, int arrayIndex)
    {
    }

    bool ICollection<Book?>.IsReadOnly
    {
        get { return false; }
    }

    bool ICollection<Book?>.Remove(Book? item)
    {
        if (InnerList.Contains(item))
        {
            InnerList.Remove(item);
            return true;
        }
        return false;
    }

    IEnumerator<Book> IEnumerable<Book?>.GetEnumerator()
    {
        return new BookCollectionEnumerator(InnerList.GetEnumerator());
    }

    private class BookCollectionEnumerator : IEnumerator<Book>
    {
        private IEnumerator _Enumerator;

        public BookCollectionEnumerator(IEnumerator enumerator)
        {
            _Enumerator = enumerator;
        }

        public Book Current
        {
            get { return (Book)_Enumerator.Current; }
        }

        object IEnumerator.Current
        {
            get { return _Enumerator.Current; }
        }

        public bool MoveNext()
        {
            return _Enumerator.MoveNext();
        }

        public void Reset()
        {
            _Enumerator.Reset();
        }

        public void Dispose()
        {
        }
    }
}

依基類變更修正

下列範例會將集合的基類從非泛型 CollectionBase 類別變更為泛 Collection<T> 型 (Collection(Of T) 在 Visual Basic 中) 類別,以修正違規。

public class Book
{
    public Book()
    {
    }
}

public class BookCollection : Collection<Book>
{
    public BookCollection()
    {
    }
}

變更已發行類別的基類會被視為現有取用者的重大變更。

另請參閱