Open sandboxFocus

Class SwitchStatementBuilder

Allows building run-time switch statements programmatically by adding case and default sections.

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

SwitchStatementBuilder provides a specialized builder for creating switch statements when the cases are determined at compile time. You initialize it with the switch expression, then use AddCase(SwitchStatementLabel, IStatementList, bool) to add case sections and AddDefault(IStatementList, bool) for the default section. Each section can optionally include a when clause for pattern matching.

For case sections that need multiple statements, create an IStatementList using List(params IStatement[]) for a fixed set of statements, or use StatementListBuilder when the statements are determined dynamically at compile time. For single-statement cases, use the overloads that accept IStatement directly.

The builder automatically handles break statements unless you specify otherwise, and supports both C# pattern matching syntax and traditional constant case labels. After adding all sections, call ToStatement() to get an IStatement that can be inserted into template code using InsertStatement(IStatement).

Constructors

Name Description
SwitchStatementBuilder(IExpression)

Initializes a new instance of the SwitchStatementBuilder class.

Properties

Name Description
SectionCount

Gets the number of sections in the switch.

Methods

Name Description
AddCase(SwitchStatementLabel, IExpression?, IStatement, bool)

Add a case switch section with a when expression. This overload accepts an IStatement.

AddCase(SwitchStatementLabel, IExpression?, IStatementList, bool)

Add a case switch section with a when expression. This overload accepts an IStatementList.

AddCase(SwitchStatementLabel, IStatement, bool)

Add a case switch section. This overload accepts an IStatement.

AddCase(SwitchStatementLabel, IStatementList, bool)

Add a case switch section. This overload accepts an IStatementList.

AddDefault(IStatement, bool)

Add a default switch section. This overload accepts an IStatement.

AddDefault(IStatementList, bool)

Add a default switch section. This overload accepts an IStatementList.

ToStatement()

Builds an IStatement from the current object.

Extension Methods

See Also