次の方法で共有


カスタム属性の使用例

更新 : 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