在使用GetProperties获取类的属性列表后,如何获取父属性的类类型?

时间:2017-04-11 19:43:30

标签: vb.net propertyinfo getproperties bindingflags

我使用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属性可能是几个层深。

1 个答案:

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

相关问题