Table of Contents

Class BlockExpressionBuilder

Namespace
Utils.Expressions
Assembly
Utils.dll

A helper class that builds expression blocks while minimizing the number of declared variables.

public class BlockExpressionBuilder
Inheritance
BlockExpressionBuilder
Inherited Members
Extension Methods

Constructors

BlockExpressionBuilder()

Initializes a new instance of the BlockExpressionBuilder class with no pre-declared variables or expressions.

public BlockExpressionBuilder()

BlockExpressionBuilder(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Initializes a new instance of the BlockExpressionBuilder class using pre-existing variables and expressions.

public BlockExpressionBuilder(IEnumerable<ParameterExpression> variables, IEnumerable<Expression> expressions)

Parameters

variables IEnumerable<ParameterExpression>

Variables that should be added to the block builder.

expressions IEnumerable<Expression>

Expressions that should initially populate the block.

Exceptions

ArgumentNullException

Thrown when variables or expressions is null.

Methods

Add(Expression)

Adds an expression to the block builder.

public Expression Add(Expression expression)

Parameters

expression Expression

The expression to add.

Returns

Expression

The same expression passed in.

AddVariable(ParameterExpression)

Adds an existing ParameterExpression to the block.

public ParameterExpression AddVariable(ParameterExpression variable)

Parameters

variable ParameterExpression

The parameter expression to add.

Returns

ParameterExpression

The same ParameterExpression that was added.

AddVariable(Type, string)

Declares a new variable of a given type and name, then adds it to the block.

public ParameterExpression AddVariable(Type type, string name)

Parameters

type Type

The variable's type.

name string

The variable's name.

Returns

ParameterExpression

The newly created ParameterExpression.

CreateBlock()

Creates an optimized block expression. If there are multiple expressions or any referenced variables, a block is created with only the necessary variables. Otherwise, a single expression is returned (or Empty() if none).

public Expression CreateBlock()

Returns

Expression

An Expression that is either a block or a single expression.

GetOrCreateVariable(Type, string)

Retrieves a variable if it already exists by name. If it does not exist, a new one is declared with the specified type and name.

public ParameterExpression GetOrCreateVariable(Type type, string name)

Parameters

type Type

The type of the variable.

name string

The name of the variable.

Returns

ParameterExpression

A ParameterExpression representing the variable.

GetVariable(string)

Returns a previously declared variable by its name.

public ParameterExpression GetVariable(string name)

Parameters

name string

The name of the variable.

Returns

ParameterExpression

The matching ParameterExpression, if found.

Exceptions

InvalidOperationException

Thrown if no variable with the specified name is found.