Esempi di utilizzo di attributi personalizzati
Aggiornamento: novembre 2007
Nell'esempio che segue viene definito un attributo personalizzato applicabile solo alle classi.
Esempio
<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
Le proprietà definite in questo attributo possono essere impostate solo dal costruttore per la classe dell'attributo. Nel codice riportato di seguito viene illustrato come è possibile utilizzare l'attributo.
' Apply the custom attribute to this class.
<Custom("Some metadata", 66)> Class ThisClass
' Add class members here.
End Class
Vedere anche
Attività
Procedura: definire attributi personalizzati
Procedura: recuperare attributi personalizzati
Concetti
Recupero di informazioni memorizzate negli attributi