title: How to create an Expression<Func<dynamic, dynamic>> - Or is it a bug?
body: <p>During my work with expression trees for a few days now, I came across something that I find difficult to understand; hopefully someone will be able so shed some light here.</p>
  
  <p>If you code <code>Expression&lt;Func&lt;dynamic, dynamic&gt;&gt; expr1 = x =&gt; 2 * x;</code> the compiler will complain and you won't get anywhere. However, it <em>seems</em> that if you create one such expression through a method then the compiler seems to be happy about it and the resulting app works. This doesn't make sense, so I'm wondering what goes on behind the curtains.</p>
  
  <p>I suppose that, under the hood, the expression returned by <code>ConvertExpression</code> is perhaps of type <code>Expression&lt;Func&lt;object, object&gt;&gt;</code>, which is a valid type, but it puzzles me that I can't use <code>Expression&lt;Func&lt;dynamic, dynamic&gt;&gt;</code> type in a declaration and yet I can use it as the return type of a method. See an example below.</p>
  
  <p>Thanks a lot!</p>
  
  <pre><code>public class ExpressionExample
  {
      public void Main()
      {
          // Doesn't compile:
          //Expression&lt;Func&lt;dynamic, dynamic&gt;&gt; expr1 = x =&gt; 2 * x;
  
          // Compiles and works - OK
          Expression&lt;Func&lt;double, double&gt;&gt; expr2 = x =&gt; 2 * x;
          Func&lt;double, double&gt; func2 = (Func&lt;double, double&gt;)expr2.Compile();
          Console.WriteLine(func2(5.0).ToString()); // Outputs 10
  
          // Compiles and works - ??? This is the confusing block...
          Expression&lt;Func&lt;dynamic, dynamic&gt;&gt; expr3 = ConvertExpression(expr2);
          Func&lt;dynamic, dynamic&gt; func3 = (Func&lt;dynamic, dynamic&gt;)expr3.Compile();
          Console.WriteLine(func3(5.0).ToString()); // Outputs 10
  
          // Side note: compiles and works:
          Expression&lt;Func&lt;object, object&gt;&gt; expr4 = x =&gt; double.Parse(2.ToString()) * double.Parse(x.ToString());
          Func&lt;object, object&gt; func4 = (Func&lt;object, object&gt;)expr4.Compile();
          Console.WriteLine(func4(5.0).ToString()); // Outputs 10
      }
  
      private Expression&lt;Func&lt;dynamic, dynamic&gt;&gt; ConvertExpression&lt;TInput, TOutput&gt;(Expression&lt;Func&lt;TInput, TOutput&gt;&gt; expression)
      {
          Expression&lt;Func&lt;object, TInput&gt;&gt; convertToInput = value =&gt; (TInput)value;
          // The following doesn't compile: var input = Expression.Parameter(typeof(dynamic), "input");
  
          var input = Expression.Parameter(typeof(object), "input");        
  
          Expression&lt;Func&lt;TOutput, dynamic&gt;&gt; convertToOutput = value =&gt; (dynamic)value;
  
          var body = Expression.Invoke(convertToOutput, Expression.Invoke(expression, Expression.Invoke(convertToInput, input)));
          var lambda = Expression.Lambda&lt;Func&lt;dynamic, dynamic&gt;&gt;(body, input);
  
          return lambda;
      }
  }
  </code></pre>