更新:2007 年 11 月
使用属性类,可以创建自己的自定义属性,使用 .NET Framework 属性同时使用这些属性类,可以提供有关程序元素的其他信息。
定义自定义属性
- 声明一个类,并将 AttributeUsageAttribute 属性应用到该类中。类的名称即为新属性的名称,如以下代码所示: - <AttributeUsage(AttributeTargets.All)> Class TestAttribute
- 声明该类从 System.Attribute 继承: - Inherits System.Attribute
- 定义 Private 字段来存储属性值: - Private m_SomeValue As String
- 需要时,请为属性创建构造函数: - Public Sub New(ByVal Value As String) m_SomeValue = Value End Sub
- 为属性 (Attribute) 定义方法、字段和属性 (Property): - Public Sub Attr(ByVal AttrValue As String) 'Add method code here. End Sub Public Property SomeValue() As String ' A named parameter. Get Return m_SomeValue End Get Set(ByVal Value As String) m_SomeValue = Value End Set End Property
- 以 End Class 构造结束该类: - End Class