Table of Contents

Class ExpressionEx

Namespace
Utils.Expressions
Assembly
Utils.dll

Provides helper methods for building and manipulating expression trees.

public static class ExpressionEx
Inheritance
ExpressionEx
Inherited Members

Methods

CreateConstant<TNumber>(TNumber)

Creates a numeric constant expression from a double value for the provided numeric type.

public static ConstantExpression CreateConstant<TNumber>(TNumber value) where TNumber : INumberBase<TNumber>

Parameters

value TNumber

Source value.

Returns

ConstantExpression

Constant expression typed as TNumber.

Type Parameters

TNumber

Numeric target type.

CreateExpressionCall(Expression, string, params Expression[])

Create an expression call on an object given the specified arguments

public static MethodCallExpression CreateExpressionCall(Expression expression, string name, params Expression[] arguments)

Parameters

expression Expression
name string
arguments Expression[]

Returns

MethodCallExpression

CreateExpressionCall(Expression, string, BindingFlags, params Expression[])

Create an expression call on an object given the specified arguments

public static MethodCallExpression CreateExpressionCall(Expression expression, string name, BindingFlags bindingFlags, params Expression[] arguments)

Parameters

expression Expression
name string
bindingFlags BindingFlags
arguments Expression[]

Returns

MethodCallExpression

CreateExpressionCall(Type, string, BindingFlags, params Expression[])

Create a static expression call given the specified arguments

public static MethodCallExpression CreateExpressionCall(Type type, string name, BindingFlags bindingFlags, params Expression[] arguments)

Parameters

type Type
name string
bindingFlags BindingFlags
arguments Expression[]

Returns

MethodCallExpression

CreateMemberExpression(Expression, MemberInfo, params Expression[])

Creates an expression accessing a specific member on an instance.

public static Expression CreateMemberExpression(Expression expression, MemberInfo member, params Expression[] arguments)

Parameters

expression Expression

Expression producing the instance whose member is accessed.

member MemberInfo

The member metadata describing the target.

arguments Expression[]

Arguments used for indexed properties or method invocations.

Returns

Expression

An Expression representing the member access.

Exceptions

ArgumentException

Thrown when the supplied arguments do not match the member signature.

CreateMemberExpression(Expression, string, params Expression[])

Creates an expression accessing a member on an instance using the supplied arguments.

public static Expression CreateMemberExpression(Expression expression, string memberName, params Expression[] arguments)

Parameters

expression Expression

Expression producing the instance whose member is accessed.

memberName string

Name of the member to access.

arguments Expression[]

Arguments used for indexed properties or method invocations.

Returns

Expression

An Expression representing the member access.

CreateMemberExpression(Expression, string, BindingFlags, params Expression[])

Creates an expression accessing a member on an instance using explicit binding flags.

public static Expression CreateMemberExpression(Expression expression, string memberName, BindingFlags bindingFlags, params Expression[] arguments)

Parameters

expression Expression

Expression producing the instance whose member is accessed.

memberName string

Name of the member to access.

bindingFlags BindingFlags

Flags controlling visibility, case, and hierarchy.

arguments Expression[]

Arguments used for indexed properties or method invocations.

Returns

Expression

An Expression representing the member access.

Exceptions

MissingMemberException

Thrown when the requested member cannot be located.

CreateStaticExpression(Type, string, params Expression[])

Creates an expression accessing a static member using the supplied arguments.

public static Expression CreateStaticExpression(Type type, string memberName, params Expression[] arguments)

Parameters

type Type

Type declaring the member.

memberName string

Name of the member to access.

arguments Expression[]

Arguments used for indexed properties or method invocations.

Returns

Expression

An Expression representing the member access.

CreateStaticExpression(Type, string, BindingFlags, params Expression[])

Creates an expression accessing a static member using explicit binding flags.

public static Expression CreateStaticExpression(Type type, string memberName, BindingFlags bindingFlags, params Expression[] arguments)

Parameters

type Type

Type declaring the member.

memberName string

Name of the member to access.

bindingFlags BindingFlags

Flags controlling visibility, case, and hierarchy.

arguments Expression[]

Arguments used for indexed properties or method invocations.

Returns

Expression

An Expression representing the member access.

Exceptions

MissingMemberException

Thrown when the requested member cannot be located.

Do(Expression, Expression, LabelTarget, LabelTarget)

Builds a do/while loop expression.

public static Expression Do(Expression test, Expression iteration, LabelTarget breakLoop = null, LabelTarget continueLoop = null)

Parameters

test Expression

Condition evaluated after each iteration.

iteration Expression

Body executed for each iteration.

breakLoop LabelTarget

Optional label used to exit the loop early.

continueLoop LabelTarget

Optional label used to jump to the next iteration.

Returns

Expression

An Expression representing the loop.

ExtractInnerExpression(LambdaExpression, params Expression[])

Replaces the parameters of a lambda expression with supplied expressions and returns the resulting body.

public static Expression ExtractInnerExpression(LambdaExpression lambda, params Expression[] replacingExpressions)

Parameters

lambda LambdaExpression

Lambda whose body will be copied.

replacingExpressions Expression[]

Expressions replacing each parameter in order.

Returns

Expression

The lambda body with parameters replaced by the provided expressions.

Exceptions

ArgumentException

Thrown when replacement counts or types do not match the lambda parameters.

For(ParameterExpression, Expression, Expression, Expression[], Expression, LabelTarget, LabelTarget)

Builds a traditional for loop expression.

public static Expression For(ParameterExpression iterator, Expression init, Expression test, Expression[] next, Expression iteration, LabelTarget breakLoop = null, LabelTarget continueLoop = null)

Parameters

iterator ParameterExpression

Loop variable that is initialized, tested, and incremented.

init Expression

Initialization expression assigning the start value.

test Expression

Condition that must remain true for the loop to continue.

next Expression[]

Expressions executed after each iteration to advance the loop.

iteration Expression

Body executed for each iteration.

breakLoop LabelTarget

Optional label used to exit the loop early.

continueLoop LabelTarget

Optional label used to jump to the next iteration.

Returns

Expression

An Expression representing the loop.

ForEach(ParameterExpression, ParameterExpression, Expression, LabelTarget, LabelTarget)

Builds a foreach-style loop over the provided enumerable expression.

public static Expression ForEach(ParameterExpression iterator, ParameterExpression enumerable, Expression iteration, LabelTarget breakLoop = null, LabelTarget continueLoop = null)

Parameters

iterator ParameterExpression

Variable that receives the current item on each iteration.

enumerable ParameterExpression

Expression that evaluates to the enumerable value.

iteration Expression

Body executed for each element.

breakLoop LabelTarget

Optional label used to exit the loop early.

continueLoop LabelTarget

Optional label used to jump to the next iteration.

Returns

Expression

An Expression representing the loop.

TryGetConverter(Expression, Expression, out Expression, BindingFlags)

Attempts to create a conversion expression between two operands using the specified binding flags.

public static bool TryGetConverter(Expression expressionLeft, Expression expressionRight, out Expression converter, BindingFlags bindingFlags = BindingFlags.Public)

Parameters

expressionLeft Expression

Expression providing the target conversion type.

expressionRight Expression

Expression representing the value to convert.

converter Expression

Receives the resulting conversion expression when successful.

bindingFlags BindingFlags

Binding flags controlling which members are considered.

Returns

bool

true when a suitable conversion expression can be created.

TryGetConverter(Type, Expression, out Expression, BindingFlags)

Attempts to create a conversion expression for the supplied target type and value expression.

public static bool TryGetConverter(Type targetType, Expression expressionRight, out Expression converter, BindingFlags bindingFlags = BindingFlags.Public)

Parameters

targetType Type

Destination type for the conversion.

expressionRight Expression

Expression representing the value to convert.

converter Expression

Receives the resulting conversion expression when successful.

bindingFlags BindingFlags

Binding flags controlling which members are considered.

Returns

bool

true when a suitable conversion expression can be created.

TryGetConverter((Type targetType, Expression expressionRight)[], out Expression, BindingFlags)

Attempts to produce a conversion expression for any of the supplied target/value pairs.

public static bool TryGetConverter((Type targetType, Expression expressionRight)[] expressions, out Expression converter, BindingFlags bindingFlags = BindingFlags.Public)

Parameters

expressions (Type targetType, Expression expressionRight)[]

Pairs describing the desired target type and associated value expression.

converter Expression

Receives the resulting conversion expression when successful.

bindingFlags BindingFlags

Binding flags controlling which members are considered.

Returns

bool

true when a suitable conversion expression can be created.

TryGetConverterMethod(Expression, Type, out Expression)

Attempts to locate an implicit or explicit conversion for the provided expression.

public static bool TryGetConverterMethod(Expression source, Type outType, out Expression builder)

Parameters

source Expression

Expression that should be converted.

outType Type

Target type of the conversion.

builder Expression

Receives the conversion expression when successful.

Returns

bool

true when a conversion was located.

TryGetConverterMethod(Expression, Type, BindingFlags, out Expression)

Attempts to locate an implicit or explicit conversion for the provided expression using the specified binding flags.

public static bool TryGetConverterMethod(Expression source, Type outType, BindingFlags bindingFlags, out Expression builder)

Parameters

source Expression

Expression that should be converted.

outType Type

Target type of the conversion.

bindingFlags BindingFlags

Binding flags controlling which members are considered.

builder Expression

Receives the conversion expression when successful.

Returns

bool

true when a conversion was located.

While(Expression, Expression, LabelTarget, LabelTarget)

Builds a while loop expression.

public static Expression While(Expression test, Expression iteration, LabelTarget breakLoop = null, LabelTarget continueLoop = null)

Parameters

test Expression

Condition that must remain true for the loop to continue.

iteration Expression

Body executed for each iteration.

breakLoop LabelTarget

Optional label used to exit the loop early.

continueLoop LabelTarget

Optional label used to jump to the next iteration.

Returns

Expression

An Expression representing the loop.