Share via


방법: 구성 요소에 대한 메타데이터 설명 제공

attributes을 통해 구성 요소에 대한 설명 메타데이터를 제공할 수 있습니다.특성은 코드 요소에 적용되는 특수한 클래스로컴파일 타임에 System.Reflection 네임스페이스를 통해 공용 언어 런타임이나 사용자 지정 도구 및 응용 프로그램에서 사용할 수 있는 메타테이터로 내보내집니다.

특성에 대한 참조를 가진 구성 요소 뒤에 관련 매개 변수 또는 플래그를 붙이는 형태로 특성이 구성 요소에 첨부됩니다.이 생성자 호출은 Visual Basic의 경우 꺾쇠괄호(<>), C#의 경우 중괄호([]) 안에 놓입니다.

관례적으로 모든 특성 클래스는 DescriptionAttribute, ObsoleteAttributeBrowsableAttribute 클래스와 같이 "Attribute"로 끝납니다.그러나 Visual Basic 및 C#를 포함하여 공용 언어 런타임을 목표로 하는 몇 가지 언어에서는 완전한 특성 이름이 필요하지 않습니다.예를 들어, ObsoleteAttribute는 코드에서 Obsolete로 참조할 수 있습니다.

기존 특성을 구성 요소에 첨부하려면

  1. 구성 요소에 필요한 특성을 결정합니다.

  2. 구성 요소에 특성을 첨부합니다.정규화된 특성 이름을 사용하거나 적절한 Imports(using) 문을 추가해야 한다는 점에 유의하십시오.다음 예제에서는 DescriptionAttribute 특성을 연결하는 방법을 보여 줍니다.

    Imports System.ComponentModel
    <Description("This is a description string")> Public Class TheClass
    End Class
    
    using System.ComponentModel;
    [Description("This is a description string")]
    public class TheClass
    {
    }
    

사용자 지정 특성

Attribute에서 상속하여 자체의 사용자 지정 도구나 응용 프로그램에서 사용할 고유 특성을 만들 수도 있습니다.응용 프로그램에 필요한 사용자 지정 속성이나 메서드를 이 기본 클래스에 추가할 수 있습니다.

사용자 지정 특성을 만들고 적용하려면

  1. Attribute에서 상속된 클래스를 만듭니다.

    Public Class WidgetAttribute
       Inherits System.Attribute
    End Class
    
    public class WidgetAttribute: System.Attribute
    {
    }
    
  2. 특성에 필요한 속성과 메서드를 결정하고 코드를 작성합니다.다음 예제에서는 WidgetAttribute 클래스의 생성자에 설정된 WidgetType 속성을 만드는 방법을 보여 줍니다.AttributeUsageAttribute 특성 특성 수를 대상으로 어떤 코드 멤버를 설정 합니다.

    <AttributeUsage(System.AttributeTargets.Class)> Public Class _
       WidgetAttribute
       Inherits System.Attribute
       Private mWidgetType as WidgetTypeEnum
       ' Creates a readonly property for the WidgetAttribute class.
       Public ReadOnly Property WidgetType as WidgetTypeEnum
          Get
             Return mWidgetType
          End Get
       End Property
       ' Creates a constructor that accepts a parameter and assigns the 
       ' value of that parameter to the WidgetType property.
       Public Sub New(type as WidgetTypeEnum)
          MyBase.New()
          mWidgetType = type
       End Sub
    End Class
    
    [AttributeUsage(System.AttributeTargets.Class)]
    public class WidgetAttribute: System.Attribute
    {
       private WidgetTypeEnum widgetType;
    
       // Creates a readonly property for the WidgetAttribute class.
       public WidgetTypeEnum WidgetType
          {
             get {return widgetType;}
          }
    
       public WidgetAttribute(WidgetTypeEnum type): base()
          {
             widgetType = type;
          }
    }
    
  3. 이 특성을 다른 특성처럼 적용하고 필요한 매개 변수를 적용합니다.

    <WidgetAttribute(WidgetTypeEnum.VerticalWidget)> _
       Public Class WidgetFortyFive
    End Class
    
    [WidgetAttribute(WidgetTypeEnum.VerticalWidget)]
    public class WidgetFortyFive
    {
    }
    

참고 항목

작업

방법: 구성 요소 속성, 메서드 및 이벤트에 메타데이터 제공

참조

Attribute

개념

사용자 지정 특성 액세스

특성에 저장된 정보 검색

기타 리소스

구성 요소에 대한 사용자 지원