title: Func<> assign a value to the result
body: <p>What I am trying to do seems simple, but I can't find a way. Let's suppose I have a method with this signature:</p>
  
  <pre><code>public void Foo&lt;T,K&gt;(Expression&lt;Func&lt;T,K&gt;&gt; binding, T item, K value)
  </code></pre>
  
  <p>What I want to do inside my method is: applying the Func to item, getting the property of type K to which the binding expression points to, and assign "value" to it.</p>
  
  <p>If the binding expression is something like:</p>
  
  <pre><code>c =&gt; c.Property
  </code></pre>
  
  <p>Where "c" is "T", and "Property" is "K", I can use some reflection easily and set the value using FieldInfo. The following code works:</p>
  
  <pre><code>(((binding as LambdaExpression).Body as MemberExpression).Member as FieldInfo).SetValue(item, value);
  </code></pre>
  
  <p>But the expression could be something like this:</p>
  
  <pre><code>c =&gt; c.SubClass.Property
  </code></pre>
  
  <p>or</p>
  
  <pre><code>c =&gt; c.SubClass1.SubClass2.SubClassN.Property
  </code></pre>
  
  <p>In this case the reflection code doesn't work, as "Property" doesn't belong directly to T, but it's still a valid lambda expression for the signature of the method.</p>
  
  <p>I know that if I compile the Func and run it on my item of type T I get the property, but it's like a "copy" of it, and not a reference of it, so even if I assign the value to it, the property on the original item doesn't get changed.</p>
  
  <p>If anyone has a clean solution to this, or just point me to something that can grant me a better knowledge of Expression Trees, you're very welcome.</p>