CA1019: 属性引数にアクセサーを定義します

プロパティ
ルール ID CA1019
Title 属性引数にアクセサーを定義します
[カテゴリ] デザイン
修正が中断ありか中断なしか なし
.NET 8 では既定で有効 いいえ

原因

そのコンストラクターでは、ある属性によって、該当プロパティを持たない引数が定義されています。

規則の説明

属性では、対象に適用するときに必ず指定する必須の引数を定義できます。 この引数は、コンストラクターに位置指定パラメーターで属性を指定できるようになるため、位置指定引数とも呼ばれます。 必須のすべての引数について、対応する読み取り専用のプロパティも属性で規定する必要があります。これは、引数値を実行時に取得できるようにするためです。 この規則では、コンストラクター パラメーターごとに、該当プロパティを定義していることが確認されます。

また、属性ではオプションの引数も定義できます。これは名前付き引数とも呼ばれます。 この引数は、名前でコンストラクターに属性を指定するときに使用されます。また、対応する読み取り/書き込みプロパティが必要です。

必須と任意の引数については、該当プロパティとコンストラクター パラメーターで同じ名前を使用してください。ただし、大文字と小文字の使い方を変えてください。 プロパティでは Pascal 形式を、パラメーターでは Camel 形式を使用します。

違反の修正方法

この規則違反を修正するには、読み取り専用プロパティが与えられていないコンストラクター パラメーターごとにそれを追加します。

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

必須引数の値を取得可能にしない場合、この規則からの警告を非表示にします。

警告を抑制する

単一の違反を抑制するだけの場合は、ソース ファイルにプリプロセッサ ディレクティブを追加して無効にしてから、規則をもう一度有効にします。

#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

詳細については、「コード分析の警告を抑制する方法」を参照してください。

使用例

カスタム属性

次の例では、必須 (位置指定) パラメーターを定義する 2 つの属性を確認できます。 属性の最初の実装が間違って定義されています。 2 番目が正しい実装です。

// 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; }
}

次の例では、カスタム属性を 2 つのプロパティに適用する方法を示しています。

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

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

CA1813: シールされていない属性を使用しません

関連項目