title: How to assign properties generically using Expression.Assign selector with target type of object?
body: <p>I am trying to use an Expression selector to generically assign properties from one type of object to another where the properties are of various types. This is the code I have so far:</p>
  
  <pre><code>var type1 = new Type1();
  var type2 = new Type2();
  
  ...
  
  var propMap = new List&lt;Tuple&lt;Expression&lt;Func&lt;Type1, object&gt;&gt;, Expression&lt;Func&lt;TradeStaticAttributesItemModel, object&gt;&gt;&gt;&gt;
      {
          new Tuple&lt;Expression&lt;Func&lt;Type1, object&gt;&gt;, Expression&lt;Func&lt;Type2, object&gt;&gt;&gt;(x =&gt; x.Prop1, x =&gt; x.Prop1),
          new Tuple&lt;Expression&lt;Func&lt;Type1, object&gt;&gt;, Expression&lt;Func&lt;Type2, object&gt;&gt;&gt;(x =&gt; x.Prop2, x =&gt; x.Prop2)
      };
  
  foreach (var prop in propMap)
  {
      if (prop.Item1.Compile()(type1) != prop.Item2.Compile()(type2))
      {
          ParameterExpression valueParameterExpression = Expression.Parameter(prop.Item2.Body.Type);
          var assign = Expression.Lambda&lt;Action&lt;Type1, object&gt;&gt;(Expression.Assign(prop.Item1.Body, valueParameterExpression), prop.Item1.Parameters.Single(), valueParameterExpression);
          Action&lt;Type1, object&gt; setter = assign.Compile();
          setter(type1, prop.Item2.Compile()(type2));
      }
  }
  </code></pre>
  
  <p>However, I am However, I'm getting the error <em>"ParameterExpression of type 'System.String' cannot be used for delegate parameter of type 'System.Object'"</em> when the type of property is a <code>string</code>. I guess this would also happen for any other type other than <code>object</code>. Any idea how to make this code work for such a situation?</p>
  
  <p>I found a potential answer <a href="https://stackoverflow.com/questions/10760139/setting-property-without-knowing-target-type-at-compile-time">here</a> using Expression.Convert, but I can't get it to work.</p>