2

カスタム属性は初めてなので、属性の値を取得できるかどうか疑問に思っています。カスタム属性を使用するクラスのプロパティの例は次のとおりです。

Private mFiller As String
<Position(378), Length(34), DataType("A"), ParticipantDependant("P/D"), RequiredProperty("Required"), Format("Blank")> _
Public Property Filler() As String
   Get
      Return mFiller
   End Get
   Set(ByVal value As String)
      mFiller = value
   End Set
End Property

これらの属性の値を取得しようとしています (つまり、位置 = 378、長さ = 34 などを取得します)。私が始めていたループは次のようになります。

Dim gwlImport As New ClientGWLImport
Dim properties() As PropertyInfo = gwlImport.GetType.GetProperties
Dim tmpInfo As PropertyInfo
For Each tmpInfo In properties
   Debug.Print("Attributes for : " & tmpInfo.Name)
   For Each tmpAttribute As Object In tmpInfo.GetCustomAttributes(True)
      Debug.Print(tmpAttribute.ToString)
   Next tmpAttribute
Next tmpInfo

これにより、すべての属性の名前が取得されますが、値を取得する方法がわかりません。何か案は?

乾杯、

ライアン

4

2 に答える 2

5

属性のタイプを知る必要があります。

例えば:

Dim posAtt As PositionAttribute 
posAtt = CType(tmpInfo.GetCustomAttributes(GetType(PositionAttribute), True)(0), PositionAttribute)
'Use some property of posAtt

ところで、ClientGWLImportそのTypeオブジェクトを取得するために new を作成する必要はありません。
代わりに、次のように書くことができます

Dim properties() As PropertyInfo = GetType(ClientGWLImport).GetProperties()
于 2009-12-21T16:14:14.887 に答える
0

System.Reflection.CustomAttributeDataクラスは、型またはメンバーを装飾するカスタム属性の完全な定義を取得する機能を公開します。

于 2009-12-21T16:57:33.887 に答える