Allows building run-time switch statements programmatically by adding case and default sections.
Implements
Namespace: Metalama.Framework.Code.SyntaxBuilders
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public sealed class SwitchStatementBuilder : IStatementBuilderRemarks
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 |
Methods
| Name | Description |
|---|---|
| AddCase(SwitchStatementLabel, IExpression?, IStatement, bool) | Add a |
| AddCase(SwitchStatementLabel, IExpression?, IStatementList, bool) | Add a |
| AddCase(SwitchStatementLabel, IStatement, bool) | Add a |
| AddCase(SwitchStatementLabel, IStatementList, bool) | Add a |
| AddDefault(IStatement, bool) | Add a |
| AddDefault(IStatementList, bool) | Add a |
| ToStatement() | Builds an IStatement from the current object. |