title: How to get all names and values of any object using reflection and recursion
body: <p>I am trying to get a property names and values from an instance of an object.  I need it to work for objects that contain nested objects where I can simple pass in the the parent instance. </p>
  
  <p>For example, if I have:</p>
  
  <pre><code>public class ParentObject
  {
      public string ParentName { get; set; }
      public NestedObject Nested { get; set; }
  }
  
  public class NestedObject
  {
      public string NestedName { get; set; }
  }
  
   // in main
   var parent = new ParentObject();
   parent.ParentName = "parent";
   parent.Nested = new NestedObject { NestedName = "nested" };                                   
  
   PrintProperties(parent); 
  </code></pre>
  
  <p>I have attempted a recursive method:</p>
  
  <pre><code>public static void PrintProperties(object obj)
  {
       var type = obj.GetType();
  
       foreach (PropertyInfo p in type.GetProperties())
       {
           Console.WriteLine(p.Name + ":- " + p.GetValue(obj, null));
  
           if (p.PropertyType.GetProperties().Count() &gt; 0)
           {              
               // what to pass in to recursive method
               PrintProperties();                                       
            }
          }
  
          Console.ReadKey();
      }
  </code></pre>
  
  <p>How do I determine that the property is then what is passed in to the PrintProperties?</p>