Class CodeBuilder
AST builder class with helpers to create various AST nodes.
public sealed class CodeBuilder
- Inheritance
-
CodeBuilder
- Extension Methods
Methods
Add(ICodeExpression, ICodeExpression)
Creates "+" binary expression.
public CodeBinary Add(ICodeExpression left, ICodeExpression right)
Parameters
left
ICodeExpressionLeft-side argument.
right
ICodeExpressionRight-side argument.
Returns
- CodeBinary
Binary operation instance.
And(ICodeExpression, ICodeExpression)
Creates logical AND binary operation.
public CodeBinary And(ICodeExpression left, ICodeExpression right)
Parameters
left
ICodeExpressionLeft-side argument.
right
ICodeExpressionRight-side argument.
Returns
- CodeBinary
Binary operation instance.
Array(IType, bool, bool, params ICodeExpression[])
Creates new array instance creation expression.
public CodeNewArray Array(IType type, bool valueTyped, bool inline, params ICodeExpression[] values)
Parameters
type
ITypeArray element type.
valueTyped
boolIndicate that array type could be infered from values, so it is not necessary to generate array element type name.
inline
boolIndicates that array should be generated on single line if possible.
values
ICodeExpression[]Array values.
Returns
- CodeNewArray
Array instance creation expression.
ArrayType(IType, bool)
Create one-dimensional array type.
public IType ArrayType(IType elementType, bool nullable)
Parameters
Returns
- IType
Array type descriptor.
As(IType, ICodeExpression)
Creates type conversion expression using as
operator.
public CodeAsOperator As(IType type, ICodeExpression expression)
Parameters
type
ITypeTarget type.
expression
ICodeExpressionCasted value expression.
Returns
- CodeAsOperator
as
operator expression.
Assign(ILValue, ICodeExpression)
Creates assignment statement.
public CodeAssignmentStatement Assign(ILValue lvalue, ICodeExpression rvalue)
Parameters
lvalue
ILValueAssignee.
rvalue
ICodeExpressionAssigned value.
Returns
- CodeAssignmentStatement
Assignment statement/expression instance.
AwaitExpression(ICodeExpression)
Creates await expression.
public CodeAwaitExpression AwaitExpression(ICodeExpression task)
Parameters
task
ICodeExpressionTask expression to await.
Returns
- CodeAwaitExpression
Await expression instance.
AwaitStatement(ICodeExpression)
Creates await statement.
public CodeAwaitStatement AwaitStatement(ICodeExpression task)
Parameters
task
ICodeExpressionTask expression to await.
Returns
- CodeAwaitStatement
Await statement instance.
Call(ICodeExpression, CodeIdentifier, params ICodeExpression[])
Create void method call statement.
public CodeCallStatement Call(ICodeExpression objOrType, CodeIdentifier method, params ICodeExpression[] parameters)
Parameters
objOrType
ICodeExpressionCallee object or type in case of static method.
method
CodeIdentifierCalled method name.
parameters
ICodeExpression[]Method parameters.
Returns
- CodeCallStatement
Call element instance.
Call(ICodeExpression, CodeIdentifier, IType, params ICodeExpression[])
Create non-void method call expression.
public CodeCallExpression Call(ICodeExpression objOrType, CodeIdentifier method, IType returnType, params ICodeExpression[] parameters)
Parameters
objOrType
ICodeExpressionCallee object or type in case of static method.
method
CodeIdentifierCalled method name.
returnType
ITypeMethod return value type.
parameters
ICodeExpression[]Method parameters.
Returns
- CodeCallExpression
Call element instance.
Call(ICodeExpression, CodeIdentifier, IType, IType[], bool, params ICodeExpression[])
Create non-void generic method call expression.
public CodeCallExpression Call(ICodeExpression objOrType, CodeIdentifier method, IType returnType, IType[] genericArguments, bool skipTypeArguments, params ICodeExpression[] parameters)
Parameters
objOrType
ICodeExpressionCallee object or type in case of static method.
method
CodeIdentifierCalled method name.
returnType
ITypeMethod return value type.
genericArguments
IType[]Generic method type arguments.
skipTypeArguments
boolType arguments could be skipped on code-generation due to type inference.
parameters
ICodeExpression[]Method parameters.
Returns
- CodeCallExpression
Call element instance.
Call(ICodeExpression, CodeIdentifier, IType[], bool, params ICodeExpression[])
Create generic void method call statement.
public CodeCallStatement Call(ICodeExpression objOrType, CodeIdentifier method, IType[] genericArguments, bool skipTypeArguments, params ICodeExpression[] parameters)
Parameters
objOrType
ICodeExpressionCallee object or type in case of static method.
method
CodeIdentifierCalled method name.
genericArguments
IType[]Generic method type arguments.
skipTypeArguments
boolType arguments could be skipped on code-generation due to type inference.
parameters
ICodeExpression[]Method parameters.
Returns
- CodeCallStatement
Call element instance.
Cast(IType, ICodeExpression)
Creates type cast expression.
public CodeTypeCast Cast(IType type, ICodeExpression value)
Parameters
type
ITypeTarget type.
value
ICodeExpressionCasted value expression.
Returns
- CodeTypeCast
Cast expression.
Commentary(string, bool)
Create code comment.
public CodeComment Commentary(string text, bool inline)
Parameters
Returns
- CodeComment
Comment instance.
Constant(bool, bool)
Creates bool constant expression.
public CodeConstant Constant(bool value, bool targetTyped)
Parameters
Returns
- CodeConstant
Constant expression instance.
Constant(int, bool)
Creates int constant expression.
public CodeConstant Constant(int value, bool targetTyped)
Parameters
Returns
- CodeConstant
Constant expression instance.
Constant(string, bool)
Creates string literal constant expression.
public CodeConstant Constant(string value, bool targetTyped)
Parameters
Returns
- CodeConstant
Constant expression instance.
Constant<T>(T, bool)
Creates enum constant expression of type T
.
public CodeConstant Constant<T>(T value, bool targetTyped) where T : Enum
Parameters
value
TConstant value.
targetTyped
boolIndicates that value is target-typed.
Returns
- CodeConstant
Constant expression instance.
Type Parameters
T
Enum type.
Default(IType, bool)
Create default expression.
public CodeDefault Default(IType type, bool targetTyped)
Parameters
type
ITypeExpression type.
targetTyped
boolIndicate that expression is target-typed and could ommit type name in generated code.
Returns
- CodeDefault
Default expression instance.
DisableWarnings(params string[])
Create suppress warning(s) pragma.
public CodePragma DisableWarnings(params string[] warnings)
Parameters
warnings
string[]Warnings to suppress.
Returns
- CodePragma
Pragma instance.
EnableNullableReferenceTypes()
Create NRT enable pragma.
public CodePragma EnableNullableReferenceTypes()
Returns
- CodePragma
Pragma instance.
Equal(ICodeExpression, ICodeExpression)
Creates equality binary expression.
public CodeBinary Equal(ICodeExpression left, ICodeExpression right)
Parameters
left
ICodeExpressionLeft-side argument.
right
ICodeExpressionRight-side argument.
Returns
- CodeBinary
Binary operation instance.
Error(string)
Create error compiler pragma.
public CodePragma Error(string errorMessage)
Parameters
errorMessage
stringError message.
Returns
- CodePragma
Pragma instance.
ExtCall(IType, CodeIdentifier, params ICodeExpression[])
Create void extension method call statement.
public CodeCallStatement ExtCall(IType type, CodeIdentifier method, params ICodeExpression[] parameters)
Parameters
type
ITypeType, containing extension method.
method
CodeIdentifierCalled method name.
parameters
ICodeExpression[]Call parameters.
Returns
- CodeCallStatement
Call element instance.
ExtCall(IType, CodeIdentifier, IType, params ICodeExpression[])
Create non-void extension method call expression.
public CodeCallExpression ExtCall(IType type, CodeIdentifier method, IType returnType, params ICodeExpression[] parameters)
Parameters
type
ITypeType, containing extension method.
method
CodeIdentifierCalled method name.
returnType
ITypeMethod return value type.
parameters
ICodeExpression[]Call parameters.
Returns
- CodeCallExpression
Call element instance.
ExtCall(IType, CodeIdentifier, IType, IType[], bool, params ICodeExpression[])
Create non-void generic extension method call expression.
public CodeCallExpression ExtCall(IType type, CodeIdentifier method, IType returnType, IType[] genericArguments, bool skipTypeArguments, params ICodeExpression[] parameters)
Parameters
type
ITypeType, containing extension method.
method
CodeIdentifierCalled method name.
returnType
ITypeMethod return value type.
genericArguments
IType[]Type arguments for generic method.
skipTypeArguments
boolType arguments could be skipped on code-generation due to type inference.
parameters
ICodeExpression[]Call parameters.
Returns
- CodeCallExpression
Call element instance.
ExtCall(IType, CodeIdentifier, IType[], bool, params ICodeExpression[])
Create void generic extension method call statement.
public CodeCallStatement ExtCall(IType type, CodeIdentifier method, IType[] genericArguments, bool skipTypeArguments, params ICodeExpression[] parameters)
Parameters
type
ITypeType, containing extension method.
method
CodeIdentifierCalled method name.
genericArguments
IType[]Generic method type arguments.
skipTypeArguments
boolType arguments could be skipped on code-generation due to type inference.
parameters
ICodeExpression[]Call parameters.
Returns
- CodeCallStatement
Call element instance.
File(string, params CodeImport[])
Add new file code unit.
public CodeFile File(string fileName, params CodeImport[] imports)
Parameters
fileName
stringFile name.
imports
CodeImport[]Using statements (imports).
Returns
- CodeFile
File code-unit.
IIF(ICodeExpression, ICodeExpression, ICodeExpression)
Creates ternary expression.
public CodeTernary IIF(ICodeExpression condition, ICodeExpression @true, ICodeExpression @false)
Parameters
condition
ICodeExpressionCondition expression.
true
ICodeExpressionCondition true value.
false
ICodeExpressionCondition false value.
Returns
- CodeTernary
Ternary expression instance.
Import(IReadOnlyList<CodeIdentifier>)
Create import statement.
public CodeImport Import(IReadOnlyList<CodeIdentifier> @namespace)
Parameters
namespace
IReadOnlyList<CodeIdentifier>Namespace to import.
Returns
- CodeImport
Import statement.
Index(ICodeExpression, ICodeExpression, IType)
Single-parameter indexed access expression.
public CodeIndex Index(ICodeExpression obj, ICodeExpression index, IType returnType)
Parameters
obj
ICodeExpressionIndexed object.
index
ICodeExpressionIndex parameter.
returnType
ITypeReturn value type.
Returns
- CodeIndex
Indexed access expression.
Lambda(IType, bool)
Creates lambda method builder.
public LambdaMethodBuilder Lambda(IType lambdaType, bool ommitTypes)
Parameters
lambdaType
ITypeLambda method type (delegate).
ommitTypes
boolLambda method could skip generation of types for parameters.
Returns
- LambdaMethodBuilder
Lambda method builder instance.
LambdaParameter(CodeIdentifier, IType)
Creates lambda method parameter without explicit type.
public CodeParameter LambdaParameter(CodeIdentifier name, IType type)
Parameters
name
CodeIdentifierParameter name.
type
ITypeParameter type.
Returns
- CodeParameter
Parameter instance.
Member(ICodeExpression, CodeReference)
Creates member access expression (e.g. property or field accessor).
public CodeMember Member(ICodeExpression obj, CodeReference member)
Parameters
obj
ICodeExpressionMember owner instance.
member
CodeReferenceMember reference.
Returns
- CodeMember
Member access expression.
Member(IType, CodeReference)
Creates static member access expression (e.g. property or field accessor).
public CodeMember Member(IType owner, CodeReference member)
Parameters
owner
ITypeStatic property owner type.
member
CodeReferenceMember reference.
Returns
- CodeMember
Property access expression.
Name(string)
Creates identifier instance.
public CodeIdentifier Name(string name)
Parameters
name
stringName to use as identifier.
Returns
- CodeIdentifier
Identifier instance.
Name(string, NameFixOptions?, int?)
Creates identifier instance with invalid identifier fix parameters.
public CodeIdentifier Name(string name, NameFixOptions? fixOptions, int? position)
Parameters
name
stringName to use as identifier.
fixOptions
NameFixOptionsOptional name fix instructions for if identifier name is not valid.
position
int?Optional identifier position (e.g. position of parameter for parameter name identifier) to use with invalid name fix options.
Returns
- CodeIdentifier
Identifier instance.
NameOf(ICodeExpression)
Creates nameof(member
) expression.
public CodeNameOf NameOf(ICodeExpression member)
Parameters
member
ICodeExpressionMember access expression, used as nameof argument.
Returns
- CodeNameOf
Nameof expression instance.
Namespace(string)
Create namespace definition.
public NamespaceBuilder Namespace(string name)
Parameters
name
stringNamespace name.
Returns
- NamespaceBuilder
Namespace builder instance.
New(IType, params ICodeExpression[])
Creates new object expression.
public CodeNew New(IType type, params ICodeExpression[] parameters)
Parameters
type
ITypeObject type to create.
parameters
ICodeExpression[]Constructor parameters.
Returns
- CodeNew
New object expression instance.
New(IType, ICodeExpression[], params CodeAssignmentStatement[])
Creates new object expression.
public CodeNew New(IType type, ICodeExpression[] parameters, params CodeAssignmentStatement[] initializers)
Parameters
type
ITypeObject type to create.
parameters
ICodeExpression[]Constructor parameters.
initializers
CodeAssignmentStatement[]Field/property initializers.
Returns
- CodeNew
New object expression instance.
Null(IType, bool)
Creates null
constant.
public CodeConstant Null(IType type, bool targetTyped)
Parameters
type
ITypeType of constant.
targetTyped
boolIndicates that constant type could be inferred from context.
Returns
- CodeConstant
Constant expression.
Parameter(IType, CodeIdentifier, CodeParameterDirection, ICodeExpression?)
Create method parameter.
public CodeParameter Parameter(IType type, CodeIdentifier name, CodeParameterDirection direction, ICodeExpression? defaultValue = null)
Parameters
type
ITypeParameter type.
name
CodeIdentifierParameter name.
direction
CodeParameterDirectionParameter direction.
defaultValue
ICodeExpressionParameter default value.
Returns
- CodeParameter
Parameter element instance.
Return(ICodeExpression?)
Creates return statement/expression.
public CodeReturn Return(ICodeExpression? expression)
Parameters
expression
ICodeExpressionOptional return value.
Returns
- CodeReturn
Return statement/expression instance.
SuppressNull(ICodeExpression)
Generate null-forgiving operator (!).
public CodeSuppressNull SuppressNull(ICodeExpression value)
Parameters
value
ICodeExpressionExpression to apply operator to.
Returns
- CodeSuppressNull
Operator expression.
Throw(ICodeExpression)
Creates throw statement.
public CodeThrowStatement Throw(ICodeExpression exception)
Parameters
exception
ICodeExpressionException object to throw.
Returns
- CodeThrowStatement
Throw statement instance.
Type(Type, bool)
Create type descriptor from Type instance.
public IType Type(Type type, bool nullable)
Parameters
Returns
- IType
Type descriptor.
TypeParameter(CodeIdentifier)
Creates generic type parameter descriptor.
public IType TypeParameter(CodeIdentifier name)
Parameters
name
CodeIdentifierType parameter name.
Returns
- IType
Type parameter descriptor instance.
Variable(CodeIdentifier, IType, bool)
Creates variable declaration.
public CodeVariable Variable(CodeIdentifier name, IType type, bool rvalueTyped)
Parameters
name
CodeIdentifierVariable name.
type
ITypeVariable type.
rvalueTyped
boolIndicates that variable is typed by assigned value and could ommit type name during code generation.
Returns
- CodeVariable
Variable declaration.