사용자 지정 특성 사용의 예

업데이트: 2007년 11월

다음 예제에서는 클래스에만 적용될 수 있는 사용자 지정 특성을 정의합니다.

예제

<AttributeUsage(AttributeTargets.Class)> Public Class CustomAttribute
    Inherits System.Attribute

    'Declare two private fields to store the property values.
    Private m_LlabelValue As String
    Private m_VValueValue As Integer

    'The Sub New constructor is the only way to set the properties.
    Public Sub New(ByVal _Label As String, ByVal _Value As Integer)
        m_LlabelValue = _Label
        m_VValueValue = _Value
    End Sub

    Public ReadOnly Property Label() As String
        Get
            Return m_LlabelValue
        End Get
    End Property

    Public ReadOnly Property Value() As Integer
        Get
            Return m_VValueValue
        End Get
    End Property
End Class

특성 클래스의 생성자만 해당 특성에 정의된 속성을 설정할 수 있습니다. 다음 코드에서는 특성을 사용하는 방법을 보여 줍니다.

' Apply the custom attribute to this class.
<Custom("Some metadata", 66)> Class ThisClass
    ' Add class members here.
End Class

참고 항목

작업

방법: 사용자 지정 특성 정의

방법: 사용자 지정 특성 검색

개념

특성의 적용

특성에 저장된 정보 검색

참조

AttributeUsageAttribute