title: Expression to create an instance with object initializer
body: <p>Is there any way to create an instance of an object with object initializer with an Expression Tree? I mean create an Expression Tree to build this lambda:</p>
  
  <pre><code>// my class
  public class MyObject {
      public bool DisplayValue { get; set; }
  }
  
  // my lambda:
  var lambda = (Func&lt;bool, MyObject&gt;)
               (displayValue =&gt; new MyObject { DisplayValue = displayValue });
  </code></pre>
  
  <p>How can I create this lambda with an Expression Tree?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tryed myself and write following code:</p>
  
  <pre><code>    public static Func&lt;bool, dynamic&gt; Creator;
  
      static void BuildLambda() {
          var expectedType = typeof(MyObject);
          var displayValueParam = Expression.Parameter(typeof(bool), "displayValue");
          var ctor = Expression.New(expectedType);
          var local = Expression.Parameter(expectedType, "obj");
          var displayValueProperty = Expression.Property(ctor, "DisplayValue");
  
          var returnTarget = Expression.Label(expectedType);
          var returnExpression = Expression.Return(returnTarget,local, expectedType);
          var returnLabel = Expression.Label(returnTarget, Expression.Default(expectedType));
  
          var block = Expression.Block(
              new[] { local },
              Expression.Assign(local, ctor),
              Expression.Assign(displayValueProperty, displayValueParam),
              Expression.Return(Expression.Label(expectedType), local, expectedType),
              returnExpression,
              returnLabel
              );
          Creator =
              Expression.Lambda&lt;Func&lt;bool, dynamic&gt;&gt;(block, displayValueParam)
                  .Compile();
      }
  </code></pre>
  
  <p>But it throws the following error:</p>
  
  <blockquote>
    <p>Cannot jump to undefined label ''.</p>
  </blockquote>
  
  <p>Can everybody help me please?</p>