Expression Class
Summary
The Expression class is the base class for all expression tree node types. It provides static factory methods for creating various types of expression nodes. These nodes are used to build expression trees, which can then be compiled and executed, or analyzed and transformed.
Methods
| Modifier | Return Type | Name | Description |
|---|---|---|---|
| static | Expression<Func<T>> |
Lambda<T>(Expression body, params ParameterExpression[] parameters) |
Creates a lambda expression. |
| static | ConstantExpression |
Constant(object value) |
Creates a constant expression. |
| static | ParameterExpression |
Parameter(Type type, string name = null) |
Creates a parameter expression. |
| static | MemberExpression |
Property(Expression instance, string propertyName) |
Creates an expression that accesses a property. |
| static | MethodCallExpression |
Call(Expression instance, MethodInfo method, params Expression[] arguments) |
Creates a method call expression. |
| static | BinaryExpression |
Add(Expression left, Expression right) |
Creates an addition expression. |
| static | TypeBinaryExpression |
TypeIs(Expression expression, Type type) |
Creates a type-is expression. |
| static | Expression<Func<T, TResult>> |
Lambda<TDelegate>(Expression body, IEnumerable<ParameterExpression> parameters) |
Creates a lambda expression from a delegate type. |
Properties
The Expression class itself primarily provides static factory methods. Specific node types derived from Expression have their own properties to represent their structure (e.g., BinaryExpression has Left and Right properties).
Remarks
Expression trees are a powerful feature in .NET that allow you to represent code as data. This enables scenarios such as LINQ providers (e.g., LINQ to SQL, LINQ to Objects), dynamic expression generation, and metaprogramming.
Example
Creating and Compiling an Expression Tree
The following example demonstrates how to create a simple expression tree that represents the expression x + 1 and then compiles and executes it.
using System;
using System.Linq.Expressions;
public class ExpressionExample
{
public static void Main(string[] args)
{
// Define the parameter for the expression tree (x)
ParameterExpression paramX = Expression.Parameter(typeof(int), "x");
// Create the constant expression for 1
ConstantExpression constant1 = Expression.Constant(1, typeof(int));
// Create the binary expression for x + 1
BinaryExpression body = Expression.Add(paramX, constant1);
// Create the lambda expression for the entire operation (x => x + 1)
Expression<Func<int, int>> lambda = Expression.Lambda<Func<int, int>>(body, new[] { paramX });
// Compile the lambda expression into an executable delegate
Func<int, int> compiledDelegate = lambda.Compile();
// Execute the compiled delegate with an input of 5
int result = compiledDelegate(5);
Console.WriteLine($"Result of x + 1 with x = 5: {result}"); // Output: Result of x + 1 with x = 5: 6
// You can also inspect the expression tree
Console.WriteLine("Expression Tree:");
Console.WriteLine(lambda);
}
}