Open sandboxFocus

Class ExpressionFactory

Provides factory methods to create IExpression objects, which are compile-time representations of run-time C# expressions.

Inheritance
ExpressionFactory
Namespace: Metalama.Framework.Code.SyntaxBuilders
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public static class ExpressionFactory
Remarks

The ExpressionFactory class provides multiple approaches to creating IExpression objects:

For complex expressions that need programmatic construction, use ExpressionBuilder instead, which provides a StringBuilder-like API for building expressions piece by piece.

Methods

Name Description
Capture(dynamic?)

Creates a compile-time object that represents a run-time expression, i.e. the syntax or code, and not the result itself. The returned IExpression can then be used in run-time C# code thanks to the Value property. This mechanism allows to generate expressions that depend on a compile-time control flow.

CastTo(IExpression, IType)

Returns an expression obtained by casting another expression to a type given as an IType.

CastTo(IExpression, Type)

Returns an expression obtained by casting another expression to a type given as a Type.

CastTo<T>(IExpression)

Returns an expression obtained by casting another expression to a type given as a generic parameter.

Default()

Gets a default expression without specifying a type. The expression will be target-typed.

Default(IType?)

Gets a default expression for a given type.

Default(SpecialType)

Gets a default expression for a given type.

Default(Type)

Gets a default expression for a given type.

Default<T>()

Gets a default expression for a given type.

Literal(byte, bool)

Returns an expression that represents a literal of type byte.

Literal(decimal, bool)

Returns an expression that represents a literal of type decimal.

Literal(double, bool)

Returns an expression that represents a literal of type double.

Literal(short, bool)

Returns an expression that represents a literal of type short.

Literal(int)

Returns an expression that represents a literal of type int.

Literal(long, bool)

Returns an expression that represents a literal of type long.

Literal(object?, bool)

Returns an expression that represents a literal.

Literal(sbyte, bool)

Returns an expression that represents a literal of type sbyte.

Literal(float, bool)

Returns an expression that represents a literal of type float.

Literal(string?, bool)

Returns an expression that represents a literal of type string.

Literal(ushort, bool)

Returns an expression that represents a literal of type ushort.

Literal(uint, bool)

Returns an expression that represents a literal of type uint.

Literal(ulong, bool)

Returns an expression that represents a literal of type ulong.

Null()

Gets a null expression without specifying a type. The expression will be target-typed.

Null(IType?)

Gets a null expression for a given type.

Null(SpecialType)

Gets a null expression for a given type.

Null(Type)

Gets a null expression for a given type.

Null<T>()

Gets a null expression for a given type.

Parse(string?, IType?, bool?)

Parses a string containing a C# expression and returns an IExpression. The Value property allows to use this expression in a template. An alternative to this method is the ExpressionBuilder class.

Receiver()

Gets an expression representing the receiver, i.e. this in an instance member, or the receiver parameter in an extension member, according to the current context.

Receiver(IDeclaration)

Gets an expression representing the receiver, i.e. this in an instance member, or the receiver parameter in an extension member, in the context of a given declaration.

This()

Gets a this expression for the current type when inside a template.

This(INamedType)

Gets a this expression for the given type.

WithNullForgivingOperator(IExpression, bool)

Returns an expression with the null-forgiving operator (!) applied, suppressing nullable warnings.

WithNullability(IExpression, bool)

Returns the same expression, but assuming it has a different nullability. This method does not generate any cast (unlike CastTo(IExpression, IType)) and should only be used when the of the nullability given expression is wrongly inferred.

WithType(IExpression, IType)

Returns the same expression, but assuming it has a different type Type. This method does not generate any cast (unlike CastTo(IExpression, IType)) and should only be used when the of the type given expression is wrongly inferred.

See Also