title: How to use an Expression<Func> to set a nested property?
body: <p>So I have some code that sets a property on an object.  This code is from an internal validation class that we're using in unit tests.  So the code may be supplied something like </p>
  
  <pre><code>private static void SetDeepValue(object targetObject, Expression&lt;Func&lt;string&gt;&gt; propertyToSet, object valueToSet)
          {
              var underlyingProperty = ((PropertyInfo)((MemberExpression)propertyToSet.Body).Member);
              underlyingProperty.SetValue(targetObject, valueToSet);
          }
  </code></pre>
  
  <p>This code gets used in a unit-test type environment, where we can then make calls like </p>
  
  <pre><code>foreach (string currentTestCaseValue in TestCaseSets)
  {
       BusinessObject myCustomer = new BusinessObject();
       SetDeepValue(myCustomer, ()=&gt;myCustomer.FirstName,currentTestCaseValue);
       ValidateBusinessRules(myCustomer);
  }
  </code></pre>
  
  <p>(code simplified for brevity/complexity)</p>
  
  <p>However, now, due to some refactorings, we are left with something like:</p>
  
  <pre><code>foreach (string currentTestCaseValue in TestCaseSets)
      {
           BusinessObject myCustomer = new BusinessObject();
           SetDeepValue(myCustomer, ()=&gt;myCustomer.NameInfo.First,currentTestCaseValue);
           ValidateBusinessRules(myCustomer);
      }
  </code></pre>
  
  <p>When this code runs, we get the error:</p>
  
  <blockquote>
    <p>Object does not match target type.</p>
  </blockquote>
  
  <p>I suspect it is trying to call the <code>First</code> property on the <code>BusinessObject</code>, instead of the <code>NameInfo</code>.   How can I modify my code to handle this 'nested' case?</p>