title: Lambda expression to initialize class with parameter and properties
body: <p>I have a method that is used to generate a <code>NewExpression</code>.</p>
  
  <pre><code>public NewExpression CreateNewExpression( Expression&lt;Func&lt;T&gt;&gt; expression )
  {
      return expression.Body as NewExpression;
  }
  </code></pre>
  
  <p>You can then do stuff like this.</p>
  
  <pre><code>CreateNewExpression( () =&gt; new MyType() );
  </code></pre>
  
  <p>This is later on used to create a delegate the can be executed to create an object.</p>
  
  <pre><code>var body = Expression.MemberInit( constructorExpression, bindings );
  var funcType = typeof( Func&lt;&gt; ).MakeGenericType( objectType );
  var lambda = Expression.Lambda( funcType, body ).Compile();
  </code></pre>
  
  <p>This works great, but now I would like to pass some data into the method so it can be used in the construction of the object.</p>
  
  <pre><code>public NewExpression CreateNewExpression( Expression&lt;Func&lt;T, Data&gt;&gt; expression )
  </code></pre>
  
  <p>This would then be used like this.</p>
  
  <pre><code>CreateNewExpression( data =&gt; new MyType( data.Id ) );
  </code></pre>
  
  <p>I can't for the life of me figure out how to create the proper expression to pass the data in for the constructor to use.</p>
  
  <p>If I don't extract the body from the expression in CreateNewExpression and do <code>Expression.Invoke</code>, I don't know how to set the properties to it after.</p>
  
  <p>If I extract the body, I can set the properties using <code>Expression.MemberInit</code>, but then I can't figure out how to pass the data to it.</p>
  
  <p>How can I do this?</p>