0

我正在使用 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作为属性可能有好几层深。

4

1 回答 1

1

我会尝试这样的方法 - 基本上是一个循环沿着继承树走......

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

于 2017-04-11T22:40:24.343 回答