title: Moq and reflection, passing dynamically generated expression tree / lambda to moq
body: <p>Is it possible to write code like the following. I'm trying to using Moq with objects that I'm reflecting on as part of a testing framework. The code below raises a "Unhandled expression type: 'Goto'" exception from Moq, which I guess is expecting something different. It kind of looks like it should work though!</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          Ifoo  = foo Foo();
  
          // Create input parameter for lambda
          ParameterExpression value = Expression.Parameter(typeof(IFoo), "value");
  
          // create return statement for lambda
          Expression setupProperty = Expression.Return(Expression.Label(), Expression.Property(value, "Bar"), typeof(string));
  
          // convert expression to lambda (should now be the equivalent of "v =&gt; v.Bar")
          var func = Expression.Lambda&lt;Func&lt;IFoo, string&gt;&gt;(setupProperty, value);//.Compile();
          //string s = func(foo); // this bit works fine in .Compile() is included
  
          var mockFoo = new Mock&lt;IFoo&gt;();
  
          mockFoo.SetupProperty(func); // exception thrown by moq here, obviously isn't exactly the same as "v =&gt; v.Bar"
          mockFoo.Object.Bar = "Burge+";
      }
  </code></pre>
  
  <p>Thanks!</p>