自定义属性用法的示例

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

只有属性 (Attribute) 类的构造函数可以设置在此属性 (Attribute) 中定义的属性 (Property)。下面的代码展示可以如何使用该属性:

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

请参见

任务

如何:定义自己的属性

如何:检索自定义属性

概念

属性的应用

检索存储在属性中的信息

参考

AttributeUsageAttribute