CA1041:必須提供 ObsoleteAttribute 訊息

屬性
規則識別碼 CA1041
標題 必須提供 ObsoleteAttribute 訊息
類別 設計
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 建議

原因

類型或成員會使用 System.ObsoleteAttribute 未指定其 System.ObsoleteAttribute.Message 屬性的屬性來標記。

根據預設,此規則只會查看外部可見的類型和成員,但這是可設定

檔案描述

ObsoleteAttribute 用來標記已被取代的程式庫類型和成員。 程式庫取用者應避免使用任何標示為過時的類型或成員。 這是因為它可能不受支援,而且最終會從更新版本的程式庫中移除。 編譯使用 ObsoleteAttribute 標記的類型或成員時, Message 就會顯示 屬性的 屬性。 以便提供使用者有關過時類型或成員的資訊。 這項資訊通常包括程式庫設計工具支援過時的類型或成員多久,以及要使用的慣用取代。

如何修正違規

若要修正此規則的違規,請將 參數新增 message 至建 ObsoleteAttribute 構函式。

隱藏警告的時機

請勿隱藏此規則的警告, Message 因為 屬性會提供過時類型或成員的重要資訊。

設定程式碼以分析

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

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

包含特定 API 介面

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

dotnet_code_quality.CAXXXX.api_surface = private, internal

範例

下列範例顯示已正確宣告 ObsoleteAttribute 的過時成員。

[ObsoleteAttribute("This property is obsolete and will be removed in a " +
"future version. Use the FullName property instead.", false)]
public string Name
{
    get => "Name";
}
Imports System

Namespace ca1041

    Public Class ObsoleteAttributeOnMember

        <ObsoleteAttribute("This property is obsolete and will " &
             "be removed in a future version. Use the FirstName " &
             "and LastName properties instead.", False)>
        ReadOnly Property Name As String
            Get
                Return "Name"
            End Get
        End Property

        ReadOnly Property FirstName As String
            Get
                Return "FirstName"
            End Get
        End Property

        ReadOnly Property LastName As String
            Get
                Return "LastName"
            End Get
        End Property

    End Class

End Namespace

另請參閱