Open sandboxFocus

Class StatementBuilder

Allows building run-time statements programmatically by composing a string using an underlying StringBuilder.

Inheritance
StatementBuilder
Implements
Namespace: Metalama.Framework.Code.SyntaxBuilders
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public sealed class StatementBuilder : SyntaxBuilder, IStatementBuilder
Remarks

StatementBuilder provides a text-based approach to constructing C# statements programmatically. It offers the same specialized methods as ExpressionBuilder for appending syntax elements (AppendLiteral(byte, bool), AppendTypeName(IType), etc.). For building multi-line code blocks, it provides methods to manage indentation and braces (BeginBlock(), EndBlock(), Indent(), Unindent()), which automatically add proper indentation after line breaks.

A major benefit of StatementBuilder is that it can be used in compile-time methods that are not templates, providing flexibility for building statements in helper methods. After building the statement string, call ToStatement() to get an IStatement object that can be inserted into template code using InsertStatement(IStatement).

When using StatementBuilder, 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
StatementBuilder()

Methods

Name Description
AppendLine()

Appends a line break.

AppendVerbatim(string)

Appends a string to the Metalama.Framework.Code.SyntaxBuilders.SyntaxBuilder.StringBuilder, without performing any modification to the input string.

BeginBlock()

Begins a block (appends a { and increments the indentation level).

Clone()

Returns a clone of the current StatementBuilder.

EndBlock()

Ends a block (appends a } and decrements the indentation level).

Indent()

Increments the indentation level.

ToStatement()

Converts the current StatementBuilder into an IStatement object, which can then be inserted into run-time code using the InsertStatement(IStatement) method.

Unindent()

Decrements the indentation level.

Extension Methods

See Also