Class ExpressionTransformer
- Namespace
- Utils.Expressions
- Assembly
- Utils.dll
Provides an abstract base class to transform or rewrite LINQ expression trees. Subclasses may override the transformation logic for specific expression signatures.
public abstract class ExpressionTransformer
- Inheritance
-
ExpressionTransformer
- Derived
- Inherited Members
- Extension Methods
Constructors
ExpressionTransformer()
Initializes a new instance of the ExpressionTransformer class. During construction, it gathers all methods marked with ExpressionSignatureAttribute from the derived type.
protected ExpressionTransformer()
Methods
CopyExpression(Expression, params Expression[])
Creates a new expression of the same ExpressionType as e,
using the supplied parameters as sub-expressions or arguments.
If certain ExpressionType values are not supported by this switch,
they are simply returned as-is or an exception is thrown.
protected static Expression CopyExpression(Expression e, params Expression[] parameters)
Parameters
eExpressionThe original expression to copy.
parametersExpression[]The sub-expressions to insert into the copied expression.
Returns
- Expression
A new expression replicating the structure of
ewith possibly different sub-expressions.
FinalizeExpression(Expression, Expression[])
Called if no custom transformation method (annotated with ExpressionSignatureAttribute) is found. Allows final post-processing. The default implementation throws an exception.
protected virtual Expression FinalizeExpression(Expression e, Expression[] parameters)
Parameters
eExpressionThe expression being finalized.
parametersExpression[]The sub-expressions or operands for
e.
Returns
- Expression
A finalized expression.
Exceptions
- Exception
Thrown by default to indicate that transformation cannot be completed.
PrepareExpression(Expression)
Prepares an expression for transformation. Subclasses can override this to apply initial logic before the main Transform(Expression) switch (e.g., caching). The default implementation returns the expression unchanged.
protected virtual Expression PrepareExpression(Expression e)
Parameters
eExpressionThe expression to prepare.
Returns
- Expression
The prepared expression.
ReplaceArguments(Expression, ParameterExpression[], Expression[])
Replaces all occurrences of oldParameters within e
with the corresponding items in newParameters.
protected Expression ReplaceArguments(Expression e, ParameterExpression[] oldParameters, Expression[] newParameters)
Parameters
eExpressionThe expression in which parameter references are replaced.
oldParametersParameterExpression[]The parameters to remove.
newParametersExpression[]The new expressions that replace
oldParameters.
Returns
- Expression
A copy of
ewhere specified parameters are replaced.
Transform(Expression)
Applies transformation rules to a given expression, returning a (potentially) modified expression. This method checks for known signatures (via ExpressionSignatureAttribute-annotated methods) and if a match is found, invokes the corresponding transformation function. If no signature method matches, it calls FinalizeExpression(Expression, Expression[]) by default.
protected Expression Transform(Expression e)
Parameters
eExpressionThe expression to transform.
Returns
- Expression
A possibly rewritten expression.