How can I read the attributes assigned to the properties of a class?
-
14-02-2021 - |
سؤال
Given the following class
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
How can I read the custom attibutes of the CustomerID
or any other property?
Thanks in advance
المحلول
Using reflection:
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
لا تنتمي إلى StackOverflow