title: C# Using Reflection to Get a Generic Object's (and its Nested Objects) Properties
body: <p>This is a scenario created to help understand what Im trying to achieve.</p>
  
  <p>I am trying to create a method that returns the specified property of a generic object</p>
  
  <p>e.g.</p>
  
  <pre><code>public object getValue&lt;TModel&gt;(TModel item, string propertyName) where TModel : class{
      PropertyInfo p = typeof(TModel).GetProperty(propertyName);
      return p.GetValue(item, null);
  }
  </code></pre>
  
  <p>The code above works fine if you're looking for a property on the <code>TModel item</code>
  e.g. </p>
  
  <pre><code>string customerName = getValue&lt;Customer&gt;(customer, "name");
  </code></pre>
  
  <p>However, if you want to find out what the customer's group's name is, it becomes a problem:
  e.g. </p>
  
  <pre><code>string customerGroupName = getValue&lt;Customer&gt;(customer, "Group.name");
  </code></pre>
  
  <p>Hoping someone can give me some insight on this way out scenario - thanks.</p>