繁体   English   中英

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

[英]How to I get the parent property's class type after using GetProperties to get a list of properties on a class?

我正在使用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/zh-cn/library/system.type.basetype(v=vs.110).aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM