Provides factory methods to create IExpression objects, which are compile-time representations of run-time C# expressions.
Namespace: Metalama.Framework.Code.SyntaxBuilders
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public static class ExpressionFactoryRemarks
The ExpressionFactory class provides multiple approaches to creating IExpression objects:
<ul><li><xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory.Literal(System.Object%2cSystem.Boolean)" data-throw-if-not-resolved="false"></xref> and overloads - Create literal expressions from compile-time values</li><li><xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory.Parse(System.String%2cMetalama.Framework.Code.IType%2cSystem.Nullable%7bSystem.Boolean%7d)" data-throw-if-not-resolved="false"></xref> - Parse C# expression strings into <xref href="Metalama.Framework.Code.IExpression" data-throw-if-not-resolved="false"></xref> objects</li><li><xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory.Capture(System.Object)" data-throw-if-not-resolved="false"></xref> - Capture the syntax of a C# expression written in a template without evaluating it</li><li><xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory.This" data-throw-if-not-resolved="false"></xref>, <xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory.Null" data-throw-if-not-resolved="false"></xref>, <xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory.Default" data-throw-if-not-resolved="false"></xref> - Create common expressions like <code>this</code>, <code>null</code>, and <code>default</code></li></ul>
For complex expressions that need programmatic construction, use <xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionBuilder" data-throw-if-not-resolved="false"></xref> instead, which provides
a StringBuilder-like API for building expressions piece by piece.
</p>
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(IType?) | Gets a |
| Default(SpecialType) | Gets a |
| Default(Type) | Gets a |
| Default<T>() | Gets a |
| 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(IType?) | Gets a |
| Null(SpecialType) | Gets a |
| Null(Type) | Gets a |
| Null<T>() | Gets a |
| 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. |
| Receiver(IDeclaration) | Gets an expression representing the receiver, i.e. |
| This() | Gets a |
| This(INamedType) | Gets a |
| WithNullForgivingOperator(IExpression, bool) | Returns an expression with the null-forgiving operator ( |
| 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. |