CA1019:定義屬性引數的存取子

屬性
規則識別碼 CA1019
標題 定義屬性引數的存取子
類別 設計
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 No

原因

在其建構函式中,屬性會定義沒有對應屬性的引數。

檔案描述

屬性可以定義必須在將屬性套用至目標時指定的強制引數。 這些引數也稱為位置引數,因為它們會當做位置參數提供給屬性建構函式。 對於每個強制引數而言,屬性 (Attribute) 還須提供對應的唯讀屬性 (Property),才可以在執行時期擷取引數值。 此規則會檢查每個建構函式參數,您已定義對應的屬性。

屬性也可以定義選擇性引數,也稱為具名引數。 這些引數會依照名稱提供給屬性 (Attribute) 建構函式,且必須有對應的讀取/寫入屬性 (Property)。

針對強制和選擇性引數,對應的屬性和建構函式參數應該使用相同的名稱,但大小寫不同。 屬性使用 Pascal 大小寫,而參數則使用駱駝大小寫。

如何修正違規

若要修正此規則的違規,請為每個沒有規則的建構函式參數新增唯讀屬性。

隱藏警告的時機

如果您不想要擷取強制引數的值,請隱藏此規則的警告。

隱藏警告

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

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

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

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

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

範例

自訂屬性

下列範例顯示兩個定義強制(位置)參數的屬性。 屬性的第一個實作未正確定義。 第二個實作正確。

// Violates rule: DefineAccessorsForAttributeArguments.
[AttributeUsage(AttributeTargets.All)]
public sealed class BadCustomAttribute : Attribute
{
    string _data;

    // Missing the property that corresponds to 
    // the someStringData constructor parameter.

    public BadCustomAttribute(string someStringData)
    {
        _data = someStringData;
    }
}

// Satisfies rule: Attributes should have accessors for all arguments.
[AttributeUsage(AttributeTargets.All)]
public sealed class GoodCustomAttribute : Attribute
{
    public GoodCustomAttribute(string someStringData)
    {
        SomeStringData = someStringData;
    }

    //The constructor parameter and property
    //name are the same except for case.

    public string SomeStringData { get; }
}
Imports System

Namespace ca1019

    ' Violates rule: DefineAccessorsForAttributeArguments.
    <AttributeUsage(AttributeTargets.All)>
    Public NotInheritable Class BadCustomAttribute
        Inherits Attribute
        Private data As String

        ' Missing the property that corresponds to 
        ' the someStringData parameter.
        Public Sub New(someStringData As String)
            data = someStringData
        End Sub 'New
    End Class 'BadCustomAttribute

    ' Satisfies rule: Attributes should have accessors for all arguments.
    <AttributeUsage(AttributeTargets.All)>
    Public NotInheritable Class GoodCustomAttribute
        Inherits Attribute

        Public Sub New(someStringData As String)
            Me.SomeStringData = someStringData
        End Sub 'New

        'The constructor parameter and property
        'name are the same except for case.

        Public ReadOnly Property SomeStringData() As String
    End Class

End Namespace

位置與具名引數

位置和具名引數可讓程式庫的取用者清楚,這些引數是屬性的必要引數,以及哪些引數是選擇性的。

下列範例示範屬性的實作,其同時具有位置引數和具名引數:

[AttributeUsage(AttributeTargets.All)]
public sealed class GoodCustomAttribute : Attribute
{
    public GoodCustomAttribute(string mandatoryData)
    {
        MandatoryData = mandatoryData;
    }

    public string MandatoryData { get; }

    public string? OptionalData { get; set; }
}

下列範例示範如何將自訂屬性套用至兩個屬性:

[GoodCustomAttribute("ThisIsSomeMandatoryData", OptionalData = "ThisIsSomeOptionalData")]
public string? MyProperty { get; set; }

[GoodCustomAttribute("ThisIsSomeMoreMandatoryData")]
public string? MyOtherProperty { get; set; }

CA1813:避免使用非密封屬性

另請參閱