本文整理汇总了VB.NET中System.Attribute类的典型用法代码示例。如果您正苦于以下问题:VB.NET Attribute类的具体用法?VB.NET Attribute怎么用?VB.NET Attribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Attribute类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: CustomAttrVB
' 导入命名空间
Imports System.Reflection
Public Module CustomAttrVB
' An enumeration of animals. Start at 1 (0 = uninitialized).
Public Enum Animal
' Pets
Dog = 1
Cat
Bird
End Enum
' Visual Basic requires the AttributeUsage be specified.
' A custom attribute to allow a target to have a pet.
<AttributeUsage(AttributeTargets.Method)> _
Public Class AnimalTypeAttribute
Inherits Attribute
' The constructor is called when the attribute is set.
Public Sub New(ByVal animal As Animal)
Me.thePet = animal
End Sub
' Keep a variable internally ...
Protected thePet As Animal
' .. and show a copy to the outside world.
Public Property Pet() As Animal
Get
Return thePet
End Get
Set(ByVal Value As Animal)
thePet = Value
End Set
End Property
End Class
' A test class where each method has its own pet.
Class AnimalTypeTestClass
<AnimalType(Animal.Dog)> _
Public Sub DogMethod()
End Sub
<AnimalType(Animal.Cat)> _
Public Sub CatMethod()
End Sub
<AnimalType(Animal.Bird)> _
Public Sub BirdMethod()
End Sub
End Class
' The runtime test.
Sub Main()
Dim testClass As New AnimalTypeTestClass()
Dim tcType As Type = testClass.GetType()
Dim mInfo As MethodInfo
' Iterate through all the methods of the class.
For Each mInfo In tcType.GetMethods()
Dim attr As Attribute
' Iterate through all the attributes of the method.
For Each attr In Attribute.GetCustomAttributes(mInfo)
If TypeOf attr Is AnimalTypeAttribute Then
Dim attrCustom As AnimalTypeAttribute = _
CType(attr, AnimalTypeAttribute)
Console.WriteLine("Method {0} has a pet {1} attribute.", _
mInfo.Name(), attrCustom.Pet.ToString())
End If
Next
Next
End Sub
End Module
' Output:
' Method DogMethod has a pet Dog attribute.
' Method CatMethod has a pet Cat attribute.
' Method BirdMethod has a pet Bird attribute.