我正在使用GetProperties来获取类的属性列表。
Dim properties As List(Of PropertyInfo) = objType.GetProperties(BindingFlags.Instance Or BindingFlags.Public).ToList()
For Each prop As PropertyInfo In properties
'how do I get the parent class type of the prop (level up in hierarchy from property's ReflectedType)?
Next
如何使父类的当前属性ReflectedType
提升一级?请注意,此类可能有多个父级别。我不想要当前属性类的BaseType
,而只是属性ReflectedType
层次结构中的下一级作为属性可能是几层深。
我会尝试这样的方法 - 基本上是一个沿着继承树向上走的循环......
Public Function WalkInheritanceFromProperty(pi As PropertyInfo) As List(Of Type)
Dim currentType As Type = pi.ReflectedType
Dim parentType As Type
Dim lst As New List(Of Type)
Do
parentType = currentType.BaseType
If Not parentType Is Nothing Then lst.Add(parentType) Else Exit Do
currentType = parentType
Loop While Not parentType Is Nothing
Return lst
End Function
以下是一些可能有帮助的信息:https://msdn.microsoft.com/en-us/library/system.type.basetype(v=vs.110(.aspx