2011-10-27 157 views
3

考虑下面的类如何读取分配给某个类的属性的属性?

Public Class Customer 
    Inherits XPBaseObject 

    Private _CustomerID As Integer = -1 

    <Key(True), _ 
    Custom("AllowNull", "True"), _ 
    Custom("AutoInc", "True"), _ 
    DbType("int")> Public Property CustomerID() As Integer 
     Get 
      Return _CustomerID 
     End Get 
     Set(ByVal value As Integer) 
      SetPropertyValue(Of Integer)("CustomerID", _CustomerID, value) 
     End Set 
    End Property 
End Class 

我怎样才能读取CustomerID或任何其他财产的习俗attibutes?

在此先感谢

回答

2

使用反射:

Dim properties As PropertyInfo() = Me.[GetType]().GetProperties() 
For Each prop As PropertyInfo In properties 
    Dim attribute As Attribute = prop.GetCustomAttributes(GetType(Attribute), True)_ 
     .OfType(Of Attribute)()_ 
     .FirstOrDefault() 
Next