다음을 통해 공유


특성을 AttributeUsageAttribute로 표시하십시오.

업데이트: 2007년 11월

TypeName

MarkAttributesWithAttributeUsage

CheckId

CA1018

범주

Microsoft.Design

변경 수준

주요 변경

원인

사용자 지정 특성에 System.AttributeUsageAttribute 특성이 없습니다.

규칙 설명

사용자 지정 특성을 정의할 때는 AttributeUsageAttribute 사용을 표시하여 사용자 지정 특성을 적용할 수 있는 소스 코드의 위치를 나타냅니다. 특성의 의미 및 용도에 따라 코드에서의 유효한 위치가 결정됩니다. 예를 들어, 라이브러리에 있는 각 형식의 유지 관리와 향상을 책임질 사람을 식별하는 특성을 정의할 때 형식 수준에서 항상 책임이 할당되는 경우 컴파일러에서는 클래스, 열거형 및 인터페이스에 대해서는 특성을 허용하지만 메서드, 이벤트 또는 속성에 대해서는 특성을 허용하지 않습니다. 특성이 어셈블리에 허용되는지 여부는 조직 정책 및 절차에 따라 결정됩니다.

System.AttributeTargets 열거형은 사용자 지정 특성에 대해 지정할 수 있는 대상을 정의합니다. AttributeUsageAttribute를 생략하는 경우 All에 정의된 것처럼 사용자 지정 특성이 모든 대상에 대해 유효하게 됩니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 AttributeUsageAttribute를 사용하여 특성의 대상을 지정합니다. 다음 예제를 참조하십시오.

경고를 표시하지 않는 경우

메시지를 제외하는 대신 이 규칙 위반 문제를 해결해야 합니다. 특성이 AttributeUsageAttribute를 상속하는 경우에도 코드 유지 관리를 단순화하기 위해 특성이 있어야 합니다.

예제

다음 예제에서는 두 개의 특성을 정의합니다. BadCodeMaintainerAttribute는 AttributeUsageAttribute 문이 생략된 올바르지 않은 특성이고 GoodCodeMaintainerAttribute는 위에서 설명한 특성을 올바르게 구현합니다. DeveloperName 속성은 디자인 규칙 특성 인수의 접근자를 정의하십시오.에 따라 필요하며 코드를 완성하기 위해 포함되었습니다.

Imports System

Namespace DesignLibrary

' Violates rule: MarkAttributesWithAttributeUsage.
NotInheritable Public Class BadCodeMaintainerAttribute
    Inherits Attribute
    Private developer As String

    Public Sub New(developerName As String)
        developer = developerName
    End Sub 'New

    Public ReadOnly Property DeveloperName() As String
        Get
            Return developer
        End Get
    End Property
End Class 

' Satisfies rule: Attributes specify AttributeUsage.
' The attribute is valid for type-level targets.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Enum Or _ 
   AttributeTargets.Interface Or AttributeTargets.Delegate)> _
NotInheritable Public Class GoodCodeMaintainerAttribute
    Inherits Attribute
    Private developer As String

    Public Sub New(developerName As String)
        developer = developerName
    End Sub 'New

    Public ReadOnly Property DeveloperName() As String
        Get
            Return developer
        End Get
    End Property
End Class 

End Namespace
using System;

namespace DesignLibrary
{
// Violates rule: MarkAttributesWithAttributeUsage.

   public sealed class BadCodeMaintainerAttribute :Attribute 
   {
      string developer;

      public BadCodeMaintainerAttribute(string developerName)
      {
         developer = developerName;
      }
      public string DeveloperName
      {
         get 
         {
            return developer;
         }
      }
   }
// Satisfies rule: Attributes specify AttributeUsage.

   // The attribute is valid for type-level targets.
   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
   public sealed class GoodCodeMaintainerAttribute :Attribute 
   {
      string developer;

      public GoodCodeMaintainerAttribute(string developerName)
      {
         developer = developerName;
      }
      public string DeveloperName
      {
         get 
         {
            return developer;
         }
      }
   }
}

관련 규칙

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

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

참고 항목

참조

특성 사용 지침

System.AttributeTargets