CA2015:請勿為衍生自 MemoryManager < T 的類型定義完成項>

屬性
規則識別碼 CA2015
標題 請勿為衍生自 MemoryManager < T 的類型定義完成項>
類別 可靠性
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 作為警告

原因

定義衍生自 的類型完成項 MemoryManager<T>

檔案描述

將完成項新增至衍生自 MemoryManager<T> 的類型可能是 Bug 的指示,因為它建議在 中 Span<T> 已分發的原生資源已清除,而且可能仍在 使用中 Span<T>

注意

類別 MemoryManager<T> 適用于進階案例。 大部分的開發人員不需要使用它。

如何修正違規

若要修正違規,請移除完成項定義。

class DerivedClass <T> : MemoryManager<T>
{
    public override bool Dispose(bool disposing)
    {
        if (disposing)
        {
            _handle.Dispose();
        }
    }

    ...

    // Violation occurs, remove the finalizer to fix the warning.
    ~DerivedClass() => Dispose(false);
}

隱藏警告的時機

如果意圖是建立完成項以進行偵錯或驗證,則隱藏此規則的違規是安全的。

隱藏警告

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

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

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

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

若要停用此整個規則類別,請將組態檔中類別的嚴重性設定為 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Reliability.severity = none

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

另請參閱