繁体   English   中英

自定义属性的VB.net值

[英]VB.net Values of Custom Attributes

我是自定义属性的新手,所以我想知道是否有可能获取属性的值。 我使用自定义属性的类中的属性的示例是:

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

我正在尝试获取那些属性的值(即,获取Position = 378,Length = 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

这为我获取了所有属性的名称,但是我不确定如何获取值。 有任何想法吗?

干杯,

瑞安

您将需要知道属性的类型。

例如:

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

顺便说一句,您无需创建新的ClientGWLImport即可获取其Type对象。
相反,您可以编写

Dim properties() As PropertyInfo = GetType(ClientGWLImport).GetProperties()

System.Reflection.CustomAttributeData类提供了用于检索装饰类型或成员的自定义属性的完整定义的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM