Expression

Class

System.Linq.Expressions

Summary

Represents a node that represents a lambda expression, which is a function that can be executed.

Members

Properties

public Type Body { get; }

Gets the expression body of the lambda expression.

public ReadOnlyCollection<ParameterExpression> Parameters { get; }

Gets the parameters of the lambda expression.

Static Methods

public static Expression Lambda<TDelegate>(Expression body, IEnumerable<ParameterExpression> parameters)

Creates a lambda expression.

Parameters:
  • Expression body: The expression body of the lambda expression.
  • IEnumerable<ParameterExpression> parameters: The parameters of the lambda expression.
Returns:

A LambdaExpression that has the specified body and parameters.

public static ParameterExpression Parameter(Type type, string name = null)

Creates a parameter expression.

Parameters:
  • Type type: The type of the parameter.
  • string name: The name of the parameter (optional).
Returns:

A ParameterExpression representing the parameter.

public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)

Creates a method call expression.

Parameters:
  • 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.
Returns:

A MethodCallExpression representing the method call.

public static ConstantExpression Constant(object value, Type type = null)

Creates a constant expression.

Parameters:
  • object value: The value of the constant.
  • Type type: The type of the constant (optional).
Returns:

A ConstantExpression representing the constant value.

Remarks

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:

You can use the static factory methods on the Expression class to build expression trees.

Example


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}");
    }
}