Building Expressions Manually
Construct expression trees with the Expression API.
Building Trees by Hand
Besides letting the compiler build a tree from a lambda, you can construct one yourself using the static factory methods on Expression.
using System;
using System.Linq.Expressions;
var five = Expression.Constant(5);
Console.WriteLine(five); // 5
Console.WriteLine(five.Type); // System.Int32Expression.Parameter
Expression.Parameter(type, name) creates a parameter node you can reference in the body and bind in a lambda.
using System;
using System.Linq.Expressions;
var x = Expression.Parameter(typeof(int), "x");
Console.WriteLine(x.Name + " : " + x.Type); // x : System.Int32All lessons in this course
- What Are Expression Trees
- Building Expressions Manually
- Compiling and Executing Expressions
- Expression Trees in LINQ Providers