views:

420

answers:

2

If I have a complex structure which contains properties which are both simple and complex types, how can I iterate over all the properties of this structure and any child properties which are not simple types?

I have a complex type called file, which contains lots of string properties and some which are other complex types which contain similar structures, eventually the whole structure breaks down into strings.

At the moment my code looks like this:

Dim file As New File
Dim props() As PropertyInfo = file.GetType.GetProperties()
_propList = New CheckBoxList

For Each prop As PropertyInfo In props

    _propList.Items.Add(prop.Name)

Next

This code loads my checkboxlist with all the names of the child properties of my file type. What I really want is a list containing all the names of properties which are of type string from all the complex types which make up the file.

I am very new to reflection so I am not sure how to approach this.

Thanks

UPDATE

Thank you for the advice so far. I have created a recursive function using vb code similar to the C# code supplied the code now looks like this:

Private Function GetStringPropertyNames(ByVal Type As System.Type) As List(Of String)

    Dim props() As PropertyInfo = Type.GetProperties
    Dim propList As New List(Of String)

    For Each prop As PropertyInfo In props

        If prop.Name <> "Chronology" And _
                prop.Name <> "Documents" And _
                prop.Name <> "Milestones" And _
                prop.Name <> "DiaryEntries" And _
                prop.Name <> "FileLoadSuccesful" And _
                prop.Name <> "FileLoadError" Then

            Dim boo As Boolean = False
            Dim bootype As Type = boo.GetType
            Dim dec As Decimal
            Dim decType As Type = dec.GetType

            If prop.PropertyType Is "".GetType Or _
                prop.PropertyType Is Now.GetType Or _
                prop.PropertyType Is bootype Or _
                prop.PropertyType Is decType Then

                propList.Add(prop.Name)

            Else

                Dim listChildPropertyStrings As List(Of String) = GetStringPropertyNames(prop.PropertyType)

                For Each strProp As String In listChildPropertyStrings

                    propList.Add(prop.Name & ": " & strProp)

                Next

            End If

        End If


    Next

    Return propList

End Function

This works, but it is ugly and I am not at all happy with certain chunks, specifically the type comparisons to establish if this is a date, string, decimal or boolean which are the potential low level types which I want to get out.

In C# it would seem these type comparisons are easier, but i seem to have to create an instance of a given type to be able to use GetType to return its type.

Can anyone advise on other ways that this code could be cleaned up.

Thanks again for your help so far.

+3  A: 

You are on the right track, Charles, just remember: "recursivity".

As you have complained about the "ugliness" of the type comparison in VB.NET, just remember that you can also compare a type as:

If TypeOf obj Is GetType(DataType) Then

Just as you'd do in C#.

Paulo Santos
Thanks for this +1, I have updated my question.
Charlie
+2  A: 

Try something lik this:

public static void GetProperties(Type t)
        {

            foreach (PropertyInfo prop in t.GetProperties())
            {
                Console.WriteLine(prop.Name);

                if (prop.PropertyType.IsClass && prop.PropertyType != typeof(String))
                {
                    GetProperties(prop.PropertyType);
                }

            }
        }
Marwan Aouida
Thanks for this +1, I have updated my question.
Charlie