views:

32

answers:

0

In .NET I have and instance of class A which includes instances of classes B,C,D and some primitives(int, double, string,bool...). I need to read(and then write to text field) all properties(recursively) of instance, if property is another instance, then read its properties etc. But I don`t want to go into .NET types(strings,...). I try to do it by reflection, but i can not finish it, please help me.

EDIT: my function - but still don`t working properly

Private Function InspectObject(ByVal instance As Object) As String
    Dim str As String 

    If TypeOf instance Is ICollection Then
        For Each member As Object In DirectCast(instance, ICollection)
            If member IsNot Nothing Then
                str += InspectObject(member)
            End If
        Next
    ElseIf TypeOf instance Is [Enum] Then
        str += vbNewLine + instance.ToString
    Else
        For Each pr As PropertyInfo In instance.GetType.GetProperties(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic)
            If pr.PropertyType.FullName.StartsWith("System.") Then

                str += vbNewLine + pr.Name + "   -   " + _
                    If(pr.GetValue(instance, Nothing) Is Nothing, "", pr.GetValue(instance, Nothing))
            Else
                If pr.GetValue(instance, Nothing) IsNot Nothing Then
                    str += vbNewLine + pr.Name
                    str += InspectObject(pr.GetValue(instance, Nothing))
                End If
            End If
        Next
    End If
    Return str
End Function

thanks a lot