title: Recursively Get Properties & Child Properties Of A Class
body: <p>I was doing something like  <a href="https://stackoverflow.com/questions/4220991/recursively-get-properties-child-properties-of-an-object">Recursively Get Properties &amp; Child Properties Of An Object</a>, but I wanted to use reflection recursively to get each properties. And I got the code from <a href="https://stackoverflow.com/a/6196516/1472828">Recursively Print the properties</a>. </p>
  
  <p>The problem with the code is: it only goes one level down, I wonder how can you automatically get all the properties using reflection? I just made up the following sample Container code:</p>
  
  <pre><code>public class Container
  {
      public Bottle MyBottle { get; set; }
      public List&lt;Address&gt; Addresses { get; set; }
  
      public Container()
      {
          Address a = new Address();
          a.AddressLine1 = "1 Main St";
          a.AddressLine2 = "2 Main St";
          Addresses = new List&lt;Address&gt;();
          Addresses.Add(a);
  
          MyBottle = new Bottle();
          MyBottle.BottleName = "Big bottle";
          MyBottle.BottageAge = 2;
      }
  }
  
  public class Bottle
  {
      public string BottleName { get; set; }
      public int BottageAge { get; set; }
  }
  
  public class Address
  {
      public string AddressLine1 { get; set; }
      public string AddressLine2 { get; set; }
      public List&lt;SpecialFolder&gt; SpecialFolders { get; set; }
  
      public Address()
      {
          SpecialFolders = new List&lt;SpecialFolder&gt;();
          SpecialFolder sf = new SpecialFolder();
          sf.TemplateFolder = Environment.SpecialFolder.Templates.ToString();
          sf.UserFolder = Environment.SpecialFolder.UserProfile.ToString();
          SpecialFolders.Add(sf);
      }
  }
  
  public class SpecialFolder
  {
      public string TemplateFolder { get; set; }
      public string UserFolder { get; set; }
  }
  </code></pre>
  
  <p>In the Main method:</p>
  
  <pre><code>static void Main(string[] args)
  {
      Container c = new Container();
      PrintProperties(c);
  }
  public static void PrintProperties(object obj)
  {
      PrintProperties(obj, 0);
  }
  public static 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);
          }
          else
          {
              Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
          }
      }
  }
  </code></pre>
  
  <p>I am hoping to get:</p>
  
  <pre><code>MyBottle:
        BottleName: Big bottle
        BottageAge: 2
  Addresses:
        AddressLine1: 1 Main St
        AddressLine2: 2 Main St
        SpecialFolders:
              TemplateFolder: Templates
              UserFolder: UserProfile
  </code></pre>
  
  <p>The result I get now:</p>
  
  <pre><code>MyBottle:
    BottleName: Big bottle
    BottageAge: 2
  Addresses: System.Collections.Generic.List`1[TreeViewReflectionExample.Address]
  </code></pre>
  
  <p>Can someone help me with the PrintProperties method? Thank you very much.</p>