Open sandboxFocus

Method GetSourceSyntax

GetSourceSyntax(IExpression)

Gets the source ExpressionSyntax of an expression that wraps source syntax, or null when the expression does not wrap source syntax, for example a generated expression or a parameter.

Declaration
public static ExpressionSyntax? GetSourceSyntax(this IExpression expression)
Parameters
Type Name Description
IExpression expression

The expression.

Returns
Type Description
ExpressionSyntax
Remarks

The method is a typed view of AsSyntaxNode. It returns the node of the source syntax tree, without the cast that the code generation can add.

The public API of Metalama.Framework does not reference Roslyn, so AsSyntaxNode is typed object, and a ISourceExpression is not always a source expression whose node is an ExpressionSyntax. This method gives code that references the SDK, for instance an extension or the provider of an interceptor, the syntax of a source expression that the code model exposes: the initializer of a field, a property or an event, or an inspection-only expression created by SourceExpressionFactory.CreateInspectionOnly, such as an argument of an intercepted call. The caller can then inspect the syntax, for instance to recognize a lambda, an interpolated string or a nameof expression, which IExpression does not describe.