カスタム属性の使用例
更新 : 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