Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating over all properties in a given type in .NET
    primarykey
    data
    text
    <p>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?</p> <p>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.</p> <p>At the moment my code looks like this:</p> <pre><code>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 </code></pre> <p>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.</p> <p>I am very new to reflection, so I am not sure how to approach this.</p> <h3>Update</h3> <p>Thank you for the advice so far. I have created a recursive function using Visual Basic code similar to the C# code supplied. The code now looks like this:</p> <pre><code>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 &lt;&gt; "Chronology" And _ prop.Name &lt;&gt; "Documents" And _ prop.Name &lt;&gt; "Milestones" And _ prop.Name &lt;&gt; "DiaryEntries" And _ prop.Name &lt;&gt; "FileLoadSuccesful" And _ prop.Name &lt;&gt; "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 &amp; ": " &amp; strProp) Next End If End If Next Return propList End Function </code></pre> <p>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.</p> <p>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.</p> <p>Are there other ways that this code could be cleaned up?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload