Table of Contents

Class CodeBuilder

Namespace
LinqToDB.CodeModel
Assembly
linq2db.Tools.dll

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 ICodeExpression

Left-side argument.

right ICodeExpression

Right-side argument.

Returns

CodeBinary

Binary operation instance.

And(ICodeExpression, ICodeExpression)

Creates logical AND binary operation.

public CodeBinary And(ICodeExpression left, ICodeExpression right)

Parameters

left ICodeExpression

Left-side argument.

right ICodeExpression

Right-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 IType

Array element type.

valueTyped bool

Indicate that array type could be infered from values, so it is not necessary to generate array element type name.

inline bool

Indicates 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

elementType IType

Array element type.

nullable bool

Type nullability.

Returns

IType

Array type descriptor.

As(IType, ICodeExpression)

Creates type conversion expression using as operator.

public CodeAsOperator As(IType type, ICodeExpression expression)

Parameters

type IType

Target type.

expression ICodeExpression

Casted value expression.

Returns

CodeAsOperator

as operator expression.

Assign(ILValue, ICodeExpression)

Creates assignment statement.

public CodeAssignmentStatement Assign(ILValue lvalue, ICodeExpression rvalue)

Parameters

lvalue ILValue

Assignee.

rvalue ICodeExpression

Assigned value.

Returns

CodeAssignmentStatement

Assignment statement/expression instance.

AwaitExpression(ICodeExpression)

Creates await expression.

public CodeAwaitExpression AwaitExpression(ICodeExpression task)

Parameters

task ICodeExpression

Task expression to await.

Returns

CodeAwaitExpression

Await expression instance.

AwaitStatement(ICodeExpression)

Creates await statement.

public CodeAwaitStatement AwaitStatement(ICodeExpression task)

Parameters

task ICodeExpression

Task 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 ICodeExpression

Callee object or type in case of static method.

method CodeIdentifier

Called 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 ICodeExpression

Callee object or type in case of static method.

method CodeIdentifier

Called method name.

returnType IType

Method 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 ICodeExpression

Callee object or type in case of static method.

method CodeIdentifier

Called method name.

returnType IType

Method return value type.

genericArguments IType[]

Generic method type arguments.

skipTypeArguments bool

Type 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 ICodeExpression

Callee object or type in case of static method.

method CodeIdentifier

Called method name.

genericArguments IType[]

Generic method type arguments.

skipTypeArguments bool

Type 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 IType

Target type.

value ICodeExpression

Casted value expression.

Returns

CodeTypeCast

Cast expression.

Commentary(string, bool)

Create code comment.

public CodeComment Commentary(string text, bool inline)

Parameters

text string

Comment text.

inline bool

Comment type: inline or single-line.

Returns

CodeComment

Comment instance.

Constant(bool, bool)

Creates bool constant expression.

public CodeConstant Constant(bool value, bool targetTyped)

Parameters

value bool

Constant value.

targetTyped bool

Indicates that value is target-typed.

Returns

CodeConstant

Constant expression instance.

Constant(int, bool)

Creates int constant expression.

public CodeConstant Constant(int value, bool targetTyped)

Parameters

value int

Constant value.

targetTyped bool

Indicates that value is target-typed.

Returns

CodeConstant

Constant expression instance.

Constant(string, bool)

Creates string literal constant expression.

public CodeConstant Constant(string value, bool targetTyped)

Parameters

value string

String value.

targetTyped bool

Indicates that value is target-typed.

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 T

Constant value.

targetTyped bool

Indicates 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 IType

Expression type.

targetTyped bool

Indicate 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 ICodeExpression

Left-side argument.

right ICodeExpression

Right-side argument.

Returns

CodeBinary

Binary operation instance.

Error(string)

Create error compiler pragma.

public CodePragma Error(string errorMessage)

Parameters

errorMessage string

Error 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 IType

Type, containing extension method.

method CodeIdentifier

Called 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 IType

Type, containing extension method.

method CodeIdentifier

Called method name.

returnType IType

Method 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 IType

Type, containing extension method.

method CodeIdentifier

Called method name.

returnType IType

Method return value type.

genericArguments IType[]

Type arguments for generic method.

skipTypeArguments bool

Type 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 IType

Type, containing extension method.

method CodeIdentifier

Called method name.

genericArguments IType[]

Generic method type arguments.

skipTypeArguments bool

Type 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 string

File 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 ICodeExpression

Condition expression.

true ICodeExpression

Condition true value.

false ICodeExpression

Condition 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 ICodeExpression

Indexed object.

index ICodeExpression

Index parameter.

returnType IType

Return value type.

Returns

CodeIndex

Indexed access expression.

Lambda(IType, bool)

Creates lambda method builder.

public LambdaMethodBuilder Lambda(IType lambdaType, bool ommitTypes)

Parameters

lambdaType IType

Lambda method type (delegate).

ommitTypes bool

Lambda 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 CodeIdentifier

Parameter name.

type IType

Parameter 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 ICodeExpression

Member owner instance.

member CodeReference

Member 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 IType

Static property owner type.

member CodeReference

Member reference.

Returns

CodeMember

Property access expression.

Name(string)

Creates identifier instance.

public CodeIdentifier Name(string name)

Parameters

name string

Name 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 string

Name to use as identifier.

fixOptions NameFixOptions

Optional 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 ICodeExpression

Member access expression, used as nameof argument.

Returns

CodeNameOf

Nameof expression instance.

Namespace(string)

Create namespace definition.

public NamespaceBuilder Namespace(string name)

Parameters

name string

Namespace name.

Returns

NamespaceBuilder

Namespace builder instance.

New(IType, params ICodeExpression[])

Creates new object expression.

public CodeNew New(IType type, params ICodeExpression[] parameters)

Parameters

type IType

Object 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 IType

Object 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 IType

Type of constant.

targetTyped bool

Indicates 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 IType

Parameter type.

name CodeIdentifier

Parameter name.

direction CodeParameterDirection

Parameter direction.

defaultValue ICodeExpression

Parameter default value.

Returns

CodeParameter

Parameter element instance.

Return(ICodeExpression?)

Creates return statement/expression.

public CodeReturn Return(ICodeExpression? expression)

Parameters

expression ICodeExpression

Optional return value.

Returns

CodeReturn

Return statement/expression instance.

SuppressNull(ICodeExpression)

Generate null-forgiving operator (!).

public CodeSuppressNull SuppressNull(ICodeExpression value)

Parameters

value ICodeExpression

Expression to apply operator to.

Returns

CodeSuppressNull

Operator expression.

Throw(ICodeExpression)

Creates throw statement.

public CodeThrowStatement Throw(ICodeExpression exception)

Parameters

exception ICodeExpression

Exception 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

type Type

Type instance.

nullable bool

Type nullability.

Returns

IType

Type descriptor.

TypeParameter(CodeIdentifier)

Creates generic type parameter descriptor.

public IType TypeParameter(CodeIdentifier name)

Parameters

name CodeIdentifier

Type 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 CodeIdentifier

Variable name.

type IType

Variable type.

rvalueTyped bool

Indicates that variable is typed by assigned value and could ommit type name during code generation.

Returns

CodeVariable

Variable declaration.