• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

.NET recursive loop through object

I have an application using iText and I need to essentially recreate the treeview of the all the properties (as per what is displayed in the debug watch list).

What I would like to do is create the treeview structure containing the data associated with the object.
1 Solution
Ivo StoykovCommented:
try something like this
public void PrintProperties(object obj)
    PrintProperties(obj, 0);
public void PrintProperties(object obj, int indent)
    if (obj == null) return;
    string indentString = new string(' ', indent);
    Type objType = obj.GetType();
    PropertyInfo[] properties = objType.GetProperties();
    foreach (PropertyInfo property in properties)
        object propValue = property.GetValue(obj, null);
        if (property.PropertyType.Assembly == objType.Assembly)
            Console.WriteLine("{0}{1}:", indentString, property.Name);
            PrintProperties(propValue, indent + 2);
            Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);

Open in new window


Ivo Stoykov
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now