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
valueTNumberSource value.
Returns
- ConstantExpression
Constant expression typed as
TNumber.
Type Parameters
TNumberNumeric 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
expressionExpressionnamestringargumentsExpression[]
Returns
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
expressionExpressionnamestringbindingFlagsBindingFlagsargumentsExpression[]
Returns
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
typeTypenamestringbindingFlagsBindingFlagsargumentsExpression[]
Returns
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
expressionExpressionExpression producing the instance whose member is accessed.
memberMemberInfoThe member metadata describing the target.
argumentsExpression[]Arguments used for indexed properties or method invocations.
Returns
- Expression
An Expression representing the member access.
Exceptions
- ArgumentException
Thrown when the supplied
argumentsdo 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
expressionExpressionExpression producing the instance whose member is accessed.
memberNamestringName of the member to access.
argumentsExpression[]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
expressionExpressionExpression producing the instance whose member is accessed.
memberNamestringName of the member to access.
bindingFlagsBindingFlagsFlags controlling visibility, case, and hierarchy.
argumentsExpression[]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
typeTypeType declaring the member.
memberNamestringName of the member to access.
argumentsExpression[]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
typeTypeType declaring the member.
memberNamestringName of the member to access.
bindingFlagsBindingFlagsFlags controlling visibility, case, and hierarchy.
argumentsExpression[]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
testExpressionCondition evaluated after each iteration.
iterationExpressionBody executed for each iteration.
breakLoopLabelTargetOptional label used to exit the loop early.
continueLoopLabelTargetOptional 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
lambdaLambdaExpressionLambda whose body will be copied.
replacingExpressionsExpression[]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
iteratorParameterExpressionLoop variable that is initialized, tested, and incremented.
initExpressionInitialization expression assigning the start value.
testExpressionCondition that must remain true for the loop to continue.
nextExpression[]Expressions executed after each iteration to advance the loop.
iterationExpressionBody executed for each iteration.
breakLoopLabelTargetOptional label used to exit the loop early.
continueLoopLabelTargetOptional 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
iteratorParameterExpressionVariable that receives the current item on each iteration.
enumerableParameterExpressionExpression that evaluates to the enumerable value.
iterationExpressionBody executed for each element.
breakLoopLabelTargetOptional label used to exit the loop early.
continueLoopLabelTargetOptional 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
expressionLeftExpressionExpression providing the target conversion type.
expressionRightExpressionExpression representing the value to convert.
converterExpressionReceives the resulting conversion expression when successful.
bindingFlagsBindingFlagsBinding flags controlling which members are considered.
Returns
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
targetTypeTypeDestination type for the conversion.
expressionRightExpressionExpression representing the value to convert.
converterExpressionReceives the resulting conversion expression when successful.
bindingFlagsBindingFlagsBinding flags controlling which members are considered.
Returns
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.
converterExpressionReceives the resulting conversion expression when successful.
bindingFlagsBindingFlagsBinding flags controlling which members are considered.
Returns
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
sourceExpressionExpression that should be converted.
outTypeTypeTarget type of the conversion.
builderExpressionReceives the conversion expression when successful.
Returns
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
sourceExpressionExpression that should be converted.
outTypeTypeTarget type of the conversion.
bindingFlagsBindingFlagsBinding flags controlling which members are considered.
builderExpressionReceives the conversion expression when successful.
Returns
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
testExpressionCondition that must remain true for the loop to continue.
iterationExpressionBody executed for each iteration.
breakLoopLabelTargetOptional label used to exit the loop early.
continueLoopLabelTargetOptional label used to jump to the next iteration.
Returns
- Expression
An Expression representing the loop.