如何读取分配给类属性的属性?

时间:2011-10-27 07:55:32

标签: .net vb.net oop class class-attributes

给出以下课程

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或任何其他属性的自定义属性?提前致谢

1 个答案:

答案 0 :(得分: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