title: How to iterate through nested properties of an object
body: <p>I am trying to loop through all properties in an object including nested objects and objects in collections, to check if the property is of DateTime data type. If it is, convert the value to UTC time leaving everything intact including the structure and the value of other properties untouched.
  The structure of my classes as follows:</p>
  
  <pre><code>public class Custom1 {
      public int Id { get; set; }
      public DateTime? ExpiryDate { get; set; }
      public string Remark { get; set; }
      public Custom2 obj2 { get; set; }
  }
  
  public class Custom2 {
      public int Id { get; set; }
      public string Name { get; set; }
      public Custom3 obj3 { get; set; }
  }
  
  public class Custom3 {
      public int Id { get; set; }
      public DateTime DOB { get; set; }
      public IEnumerable&lt;Custom1&gt; obj1Collection { get; set; }
  }
  
  public static void Main() {
      Custom1 obj1 = GetCopyFromRepository&lt;Custom1&gt;();
  
      // this only loops through the properties of Custom1 but doesn't go into properties in Custom2 and Custom3
      var myType = typeof(Custom1);
      foreach (var property in myType.GetProperties()) {
          // ...
      }
  }
  </code></pre>
  
  <p>How do I loop through the properties in <strong>obj1</strong> and traverse further down <strong>obj2</strong> then <strong>obj3</strong> then <strong>obj1Collection</strong>? The function have to be generic enough because the Type passed to the function cannot be determined at design/compile time. Conditional statements to test for Types should be avoided since they might be class <strong>Custom100</strong></p>
  
  <pre><code>//avoid conditional statements like this
  if (obj is Custom1) {
      //do something
  } else if (obj is Custom2) {
      //do something else
  }  else if (obj is Custom3) {
      //do something else
  } else if ......
  </code></pre>