元数据--自定义属性(VB.NET)

类文件:(Class1)
Imports System
Imports System.Reflection

 1<attributeusage(attributetargets.all, allowmultiple:="True," inherited:="True)"> _   
 2Public Class Class1   
 3Inherits System.Attribute   
 4Private FamilyName As String   
 5Private GivenName As String   
 6Public Sub New(ByVal FamilyName As String)   
 7Me.FamilyName = FamilyName   
 8End Sub   
 9Public Overrides Function ToString() As String   
10Return String.Format("Author:{0}{1}", FamilyName, GivenName)   
11End Function   
12Public Property Family() As String   
13Get   
14Return FamilyName   
15End Get   
16Set(ByVal Value As String)   
17FamilyName = Value   
18End Set   
19End Property   
20Public Property Given() As String   
21Get   
22Return GivenName   
23End Get   
24Set(ByVal Value As String)   
25GivenName = Value   
26End Set   
27End Property   
28End Class 
29
30* * *
31
32  
33使用自定义属性的文件(Form3.vb)   
34  
35<class1("watkins", ),="" class1("abrams")="" given:="Damien"> _   
36Public Class Form3   
37Inherits System.Windows.Forms.Form   
38  
39Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
40Dim t As Type = Type.GetType("WindowsApplication6.Form3")   
41Dim attributes As Object() = t.GetCustomAttributes(True)   
42Console.WriteLine("Custom Attributes are: ")   
43For Each o As Object In attributes   
44Console.WriteLine(o)   
45Next   
46End Sub   
47End Class</class1("watkins",></attributeusage(attributetargets.all,>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus