title: Deep Clone with Expression.New and Expression Trees
body: <p>I have two Generated Interfaces <code>IPerson</code> and <code>IAddress</code>.</p>
  
  <p>However I have defined Property Interfaces which Inherit from those base Interfaces</p>
  
  <h2>Interfaces</h2>
  
  <pre><code>public interface IPerson_Name : IPerson { String Name{get;set;}}
  
  public interface IPerson_Addresses : IPerson
  {
      ICollection&lt;IAddress&gt; Addresses{ get; set; }
      IAddress NewAddress();
  }
  public interface IAddress_Line1 : IAddress
  {
      String Line1 { get; set; }
  }
  </code></pre>
  
  <p>Then I have two implementations of each of the base interfaces</p>
  
  <h2>Implementation</h2>
  
  <pre><code>public class Person : IPerson
  {
      public String Name { get; set; }
      public ICollection&lt;Address&gt; Addresses { get; set; }
  }
  public class PersonPoco : IPerson_Name, IPerson_Addresses
  {
      public string Name { get; set; }
      public ICollection&lt;IAddress&gt; Addresses { get; set; }
      public IAddress NewAddress()
      {
          return new AddressPoco();
      }
  }
  public class Address : IAddress
  {
      public String Line1 { get; set; }
  }
  public class AddressPoco : IAddress_Line1
  {
      public String Line1 { get; set; }
  }
  </code></pre>
  
  <p>I'm trying to build expression trees to Convert between PersonPoco and Person as well as any other IPerson</p>
  
  <p>I'm creating a CopyTo function as such.</p>
  
  <h2>Copy Extenstion</h2>
  
  <pre><code>public static class IPersonExt
  {
      public static IQueryable&lt;TDest&gt; CopyTo&lt;TSrc,TDest&gt;(this IQueryable&lt;TSrc&gt; persons) 
          where TSrc : IPerson, new()
          where TDest: IPerson, new()
      {
  
          var innerLambda = (LambdaExpression)CopyTo(typeof (TSrc), typeof (TDest));
          var copyExpr = Expression.Lambda&lt;Func&lt;TSrc, TDest&gt;&gt;(innerLambda.Body, innerLambda.Parameters);
          return persons.Select(copyExpr);
      }
      internal static LambdaExpression CopyTo(Type tSrc, Type tDest)
      {
          var dest = Activator.CreateInstance(tDest);
          var personparam = Expression.Parameter(tSrc);
  
          var destNewExpr = Expression.New(tDest);
          var memberbindings = new List&lt;MemberBinding&gt;();
  
          IPerson_Name destName;
          IPerson_Name srcName;
          if ((tDest.IsInstanceOfType(typeof(IPerson_Name)) || tDest.IsInstanceOfType(typeof(Person))) &amp;&amp;
              (tSrc.IsInstanceOfType(typeof(IPerson_Name)) || tSrc.IsInstanceOfType(typeof(Person))))
          {
              memberbindings.Add(Expression.Bind(tDest.GetProperty("Name"), Expression.Property(personparam, "Name")));
          }
  
          var toEntityParameterExpression = Expression.MemberInit(destNewExpr, memberbindings);
  
          return Expression.Lambda(
                  toEntityParameterExpression,
                  personparam
              );
      }
  }
  </code></pre>
  
  <p>My intention is to save the compiled output for every implementation to implementation conversion of IPerson and IAddress.</p>
  
  <p>If you want more info about my intentions see <a href="https://stackoverflow.com/questions/21565703/entity-framework-iqueryable-with-poco-generation">this question</a></p>
  
  <h2>Edit</h2>
  
  <p>I got the base parameter copying to a new version. It's working so far for basic properties. I've updated the code above</p>