Represents a node that represents a lambda expression, which is a function that can be executed.
public Type Body { get; }Gets the expression body of the lambda expression.
public ReadOnlyCollection<ParameterExpression> Parameters { get; }Gets the parameters of the lambda expression.
public static Expression Lambda<TDelegate>(Expression body, IEnumerable<ParameterExpression> parameters) Creates a lambda expression.
Expression body: The expression body of the lambda expression.IEnumerable<ParameterExpression> parameters: The parameters of the lambda expression.A LambdaExpression that has the specified body and parameters.
public static ParameterExpression Parameter(Type type, string name = null)Creates a parameter expression.
Type type: The type of the parameter.string name: The name of the parameter (optional).A ParameterExpression representing the parameter.
public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)Creates a method call expression.
Expression instance: The object on which to call the method.MethodInfo method: The method to call.IEnumerable<Expression> arguments: The arguments to the method call.A MethodCallExpression representing the method call.
public static ConstantExpression Constant(object value, Type type = null)Creates a constant expression.
object value: The value of the constant.Type type: The type of the constant (optional).A ConstantExpression representing the constant value.
The Expression class serves as the base class for all expression types in the System.Linq.Expressions namespace. It provides a way to represent code as a data structure, enabling dynamic code generation, compilation, and execution.
Key expression types include:
ConstantExpression: Represents a constant value.ParameterExpression: Represents a parameter in an expression.MethodCallExpression: Represents a call to a method.LambdaExpression: Represents a lambda expression.You can use the static factory methods on the Expression class to build expression trees.
using System;
using System.Linq.Expressions;
using System.Reflection;
public class Example
{
public static void Main(string[] args)
{
// Create a parameter expression for an integer
ParameterExpression numParam = Expression.Parameter(typeof(int), "num");
// Create a constant expression with the value 5
ConstantExpression five = Expression.Constant(5);
// Create a binary expression representing num + 5
BinaryExpression add = Expression.Add(numParam, five);
// Create a lambda expression representing the function delegate 'Func'
// The lambda takes 'num' as input and returns 'num + 5'
Expression> addFiveLambda = Expression.Lambda>(add, new ParameterExpression[] { numParam });
// Compile the lambda expression into an executable delegate
Func compiledAddFive = addFiveLambda.Compile();
// Execute the compiled delegate
int result = compiledAddFive(10); // result will be 15
Console.WriteLine($"Result of adding 5 to 10: {result}");
// Example of a method call expression
MethodInfo toStringMethod = typeof(object).GetMethod("ToString", new Type[] {});
MethodCallExpression toStringCall = Expression.Call(add, toStringMethod);
Expression> convertToStringLambda = Expression.Lambda>(toStringCall, new ParameterExpression[] { numParam });
Func compiledConvertToString = convertToStringLambda.Compile();
string stringResult = compiledConvertToString(25);
Console.WriteLine($"Result of converting 25 to string: {stringResult}");
}
}