언어

CA1019: 특성 인수의 접근자를 정의하십시오.

속성 값
규칙 ID CA1019
제목 특성 인수의 접근자를 정의하세요.
범주 디자인
수정 사항이 호환성을 깨뜨리는지 여부 또는 무중단인지 여부 주요 변경 아님
.NET 10에서 기본적으로 사용하도록 설정 아니요
적용 가능한 언어 C# 및 Visual Basic

원인

해당 생성자에서 특성은 해당 속성이 없는 인수를 정의합니다.

규칙 설명

특성에서는 대상에 특성을 적용할 때 지정해야 하는 필수 인수를 정의할 수 있습니다. 이러한 인수는 특성 생성자에 위치 매개 변수로 제공되기 때문에 이러한 인수를 위치 인수라고도 합니다. 모든 필수 인수에 대해 특성은 실행 시간에 인수의 값을 검색할 수 있도록 해당하는 읽기 전용 속성도 제공해야 합니다. 이 규칙은 각 생성자 매개 변수에 대해 해당 속성을 정의했는지 확인합니다.

특성에서는 명명된 인수라고 하는 선택적 인수도 정의할 수 있습니다. 이들 인수는 이름으로 특성 생성자에 제공되며 해당하는 읽기/쓰기 특성이 있어야 합니다.

필수 인수 및 선택적 인수의 경우 해당 속성 및 생성자 매개 변수가 같은 이름(대/소문자는 달라야 함)을 사용해야 합니다. 속성은 파스칼식 대문자/소문자 표기법을 사용하며, 매개변수는 카멜식 대문자/소문자 표기법을 사용합니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 생성자 매개변수 중에서 읽기 전용 속성이 없는 항목에 읽기 전용 속성을 추가합니다.

경고를 표시하지 않는 경우

필수 인수의 값을 검색할 수 없게 하려면 이 규칙의 경고를 표시하지 마세요.

경고 표시 안 함

단일 위반을 억제하려면 원본 파일에 전처리기 지시문을 추가하여 규칙을 비활성화한 후 다시 활성화하십시오.

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

다음 예제에서는 사용자 지정 특성을 2가지 속성에 적용하는 방법을 보여 줍니다.

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

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

CA1813: 봉인되지 않은 특성을 사용하지 마십시오.

참고하기