Open sandboxFocus

Method Capture

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.

Declaration
[CompileTime(true, null)]
public static IExpression Capture(dynamic? expression)
Parameters
Type Name Description
dynamic expression

A run-time expression, possibly containing compile-time sub-expressions. The expression cannot be dynamic. If you have a dynamic expression, do not call this method, but cast the dynamic expression to IExpression.

Returns
Type Description
IExpression
Remarks

The Capture(dynamic?) method is the simplest way to create an IExpression in a template. It captures the C# syntax tree of an expression without evaluating it. This is particularly useful for creating expressions that depend on compile-time conditions.

When the compile-time type of the expression to capture is dynamic, it must be explicitly cast to IExpression instead of using this method. This is a workaround for C# language limitations.

See Also