2017-04-11 17 views
0

私はクラスのプロパティのリストを取得するためにGetPropertiesを使用しています。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の親クラスの1レベルアップを得るのですか?このクラスは複数の親レベルを持つことができます。私は現在のプロパティのクラスのBaseTypeを望んでいませんが、プロパティの階層内の次のレベルは、プロパティとしていくつかの層が深くなる可能性があるので、ReflectedTypeです。

答えて

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

関連する問題