I'm trying to figure out how to put all the pieces together, and would appreciate a concrete source code sample for a simple case to start with.
Consider the following C# code:
Func<int, int, int> f = (x, y) => x + y;
I can produce an equivalent function at runtime using expression trees as follows:
var x = Expression.Parameter(typeof(int), "x");
var y = Expression.Parameter(typeof(int), "y");
Func<int, int, int> f =
Expression.Lambda<Func<int, int, int>>(
Expression.Add(x, y),
new[] { x, y }
).Compile();
Now given the following lambda:
Func<dynamic, dynamic, dynamic> f = (x, y) => x + y;
how would I generate the equivalent using expression trees (and, presumably, Expression.Dynamic
)?