Allows to build a run-time expression programmatically by composing a string using an underlying StringBuilder.
Implements
Namespace: Metalama.Framework.Code.SyntaxBuilders
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public sealed class ExpressionBuilder : SyntaxBuilder, IExpressionBuilderRemarks
ExpressionBuilder provides a text-based approach to constructing complex C# expressions programmatically or dynamically. It offers specialized methods for appending different syntax elements: AppendLiteral(byte, bool) for literals, AppendTypeName(IType) for fully-qualified type names, AppendExpression(dynamic?) for existing expressions, and AppendVerbatim(string) for keywords and punctuation.
A major benefit of ExpressionBuilder is that it can be used in compile-time methods that are not templates,
providing flexibility for building expressions in helper methods. After building the expression string, call
ToExpression() to get an IExpression object, or use ToValue(INotNullExpressionBuilder)
to get a dynamic value that can be used directly in template code.
When using ExpressionBuilder, ensure that all type names are fully namespace-qualified, as you cannot assume
the target code has any required using directives. Metalama will simplify the code and add relevant using
directives when producing formatted output.
Constructors
| Name | Description |
|---|---|
| ExpressionBuilder() |
Properties
| Name | Description |
|---|---|
| ExpressionType | Gets or sets the resulting type of the expression, if known. This value allows to generate simpler code. |
| IsReferenceable | Gets or sets a value indicating whether the expression can be used in |
Methods
| Name | Description |
|---|---|
| Clone() | Returns a clone of the current ExpressionBuilder. |
| ToExpression() | Creates a compile-time IExpression from the current ExpressionBuilder. |