カスタム属性

属性は、ClassやMethod、property等に、任意のパラメータを追加することができる。

追加した内容をリフレクションで読取、使用することが可能。

例えば以下は、TClassの各Propertyに、画面に表示する際の列名、Export時のヘッダ名、

また、出力の有無といった情報を持たせるカスタム属性と、それをリフレクションで

取得する例

■カスタム属性クラス

<AttributeUsage(AttributeTargets.Property, AllowMultiple:=True)>
Public Class DisplayInfomationAttribute
Inherits Attribute

Public Property DisplayHeaderText As String
Public Property FileHeaderText As String
Public Property DisplayShow As Boolean
Public Property Export As Boolean

Public Sub New(ByVal displayHeaderText As String, ByVal fileHeaderText As String, ByVal displayShow As Boolean, ByVal export As Boolean)
Me.DisplayHeaderText = displayHeaderText
Me.FileHeaderText = fileHeaderText
Me.DisplayShow = displayShow
Me.Export = export
End Sub
End Class

■リフレクション例

Public Shared Sub SetDgvHeaderext(ByVal dgv As DataGridView, tBase As ITData)
'---Tclassのpropertyを取得
For Each prop As PropertyInfo In tBase.GetType().GetProperties()
 dgv.Columns(prop.Name).Visible = False
 dgv.Columns(prop.Name).HeaderText = ""
 ’---propertyの属性を取得

 For Each wrk As DisplayInfomationAttribute In prop.GetCustomAttributes(True)
  dgv.Columns(prop.Name).Visible = wrk.DisplayShow
  dgv.Columns(prop.Name).HeaderText = wrk.DisplayHeaderText
 Next
Next
End Sub