title: Getting Nested Object Property Value Using Reflection
body: <p>I have the following two classes:</p>
  
  <pre><code>public class Address
  {
      public string AddressLine1 { get; set; }
      public string AddressLine2 { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string Zip { get; set; }
  }
  
  public class Employee
  {
      public string FirstName { get; set; }
      public string MiddleName { get; set; }
      public string LastName { get; set; }
      public Address EmployeeAddress { get; set; }
  }
  </code></pre>
  
  <p>I have an instance of the employee class as follows:</p>
  
  <pre><code>    var emp1Address = new Address();
      emp1Address.AddressLine1 = "Microsoft Corporation";
      emp1Address.AddressLine2 = "One Microsoft Way";
      emp1Address.City = "Redmond";
      emp1Address.State = "WA";
      emp1Address.Zip = "98052-6399";
  
      var emp1 = new Employee();
      emp1.FirstName = "Bill";
      emp1.LastName = "Gates";
      emp1.EmployeeAddress = emp1Address;
  </code></pre>
  
  <p>I have a method which gets the property value based on the property name as follows:</p>
  
  <pre><code>public object GetPropertyValue(object obj ,string propertyName)
  {
      var objType = obj.GetType();
      var prop = objType.GetProperty(propertyName);
  
      return prop.GetValue(obj, null);
  }
  </code></pre>
  
  <p>The above method works fine for calls like <code>GetPropertyValue(emp1, "FirstName")</code> but if I try <code>GetPropertyValue(emp1, "Address.AddressLine1")</code> it throws an exception because <code>objType.GetProperty(propertyName);</code> is not able to locate the nested object property value. Is there a way to fix this?</p>