1: <AttributeUsage(AttributeTargets.Class)> Public Class AuthorAttribute
2: Inherits Attribute
3:
4: Private _name As String
5: Private _organization As String
6:
7: Public Sub New(ByVal name As String)
8: _name = name
9: _organization = "(no organization)"
10: End Sub
11:
12: Public ReadOnly Property name() As String
13: Get
14: Return _name
15: End Get
16: End Property
17:
18: Public Property organization() As String
19: Get
20: Return _organization
21: End Get
22: Set(ByVal Value As String)
23: _organization = Value
24: End Set
25: End Property
26: End Class
27:
28: <Author("Ichiro")> Public Class Test1
29: End Class
30:
31: <Author("Jiro", organization:="The Program Company")> Public Class Test2
32: End Class
33:
34: <Author("Saburo", organization:="The Software Group")> Public Class Form1
35: Inherits System.Windows.Forms.Form
36:
37: …Windows フォーム デザイナで生成されたコード…
38:
39: Public Sub dumpAuthor(ByVal className As String)
40: Dim targetType As Type = Type.GetType("Sample008n." + className)
41: Dim list() As Object = targetType.GetCustomAttributes(GetType(AuthorAttribute), False)
42: Dim item As AuthorAttribute
43: For Each item In list
44: Trace.WriteLine("class " & className & " is written by " & item.name & " in " & item.organization)
45: Next
46: End Sub
47:
48: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
49: dumpAuthor("Test1")
50: dumpAuthor("Test2")
51: dumpAuthor("Form1")
52: End Sub
53: End Class
リスト14-22 名前付き引数をサポートした属性を記述したプログラム
これを実行すると以下のようになる。
1: class Test1 is written by Ichiro in (no organization)
2: class Test2 is written by Jiro in The Program Company
3: class Form1 is written by Saburo in The Software Group
1: Imports System.Web.Services
2:
3: <WebService(Namespace := "http://tempuri.org/")> _
4: Public Class Service1
5: Inherits System.Web.Services.WebService
6:
7: …Web サービス デザイナで生成されたコード…
8:
9: ' WEB SERVICE EXAMPLE
10: ' HelloWorld() サービスのサンプルは文字列 Hello World を返します。
11: ' ビルドするには、以下の行からコメントを削除して保存してからプロジェクトをビルドします。
12: ' この Web サービスをテストするには、.asmx ファイルがスタートページに設定されていることを確認し、
13: ' F5 キーを押してください。
14: '
15: '<WebMethod()> Public Function HelloWorld() As String
16: ' HelloWorld = "Hello World"
17: ' End Function
18:
19: End Class