title: Expression Tree copying object
body: <p>Trying to create an expression tree to do an object mapper type thing.</p>
  
  <pre><code>Type ts = typeof(Source);
  Type td = typeof(Dest);
  
  ParameterExpression val = Expression.Parameter(ts);
  ParameterExpression ret = Expression.Parameter(td);
  
  PropertyInfo[] propsS = ts.GetProperties();
  PropertyInfo[] propsD = td.GetProperties();
  
  List&lt;Expression&gt; lst = new List&lt;Expression&gt;();
  
  foreach (PropertyInfo pi in propsS)
  {
      PropertyInfo piD = propsD.Where(x =&gt; x.Name == pi.Name).FirstOrDefault();
  
      if (piD != null)
      {
          MethodInfo ge = pi.GetGetMethod();
          MethodInfo se = piD.GetSetMethod();
          var v1 = Expression.Call(val, ge);
          var v2 = Expression.Call(ret, se, v1);
          lst.Add(v2);
      }
  }
  
  lst.Add(Expression.Return(Expression.Label(td), ret));
  
  BlockExpression block = Expression.Block(
          new[] { ret },
          lst.ToArray()
      );
  
  //Func&lt;Source, Dest&gt; v = Expression.Lambda&lt;Func&lt;Source, Dest&gt;&gt;(block, val).Compile();
  var v = Expression.Lambda(block, val);
  </code></pre>
  
  <p>So that's what I have now... its very close, but don't see what I'm missing...</p>
  
  <p>v comes out to:</p>
  
  <pre><code>.Lambda #Lambda1&lt;System.Action`1[ConsoleApplication2.Source]&gt;(ConsoleApplication2.Source $var1) {
      .Block(ConsoleApplication2.Dest $var2) {
          .Call $var2.set_S1(.Call $var1.get_S1());
          .Call $var2.set_S2(.Call $var1.get_S2());
          .Call $var2.set_I1(.Call $var1.get_I1());
          .Call $var2.set_I2(.Call $var1.get_I2());
          .Call $var2.set_S3(.Call $var1.get_S3());
          .Call $var2.set_S4(.Call $var1.get_S4());
          .Call $var2.set_S5(.Call $var1.get_S5());
          .Return #Label1 { $var2 }
      }
  }
  </code></pre>
  
  <ol>
  <li>Do I need to new up $var2 somewhere?</li>
  <li>Is there a better way to do the assigns?</li>
  <li>The lambda doesn't seem to see the return value...</li>
  <li>Do I need to do the block? or is there a better way?</li>
  </ol>