Open sandboxFocus

Method RewriteSyntaxTreesAsync

RewriteSyntaxTreesAsync(CSharpSyntaxRewriter, CancellationToken)

Rewrites all syntax trees in the compilation using a shared CSharpSyntaxRewriter.

Declaration
public Task RewriteSyntaxTreesAsync(CSharpSyntaxRewriter rewriter, CancellationToken cancellationToken = default)
Parameters
Type Name Description
CSharpSyntaxRewriter rewriter

A thread-safe CSharpSyntaxRewriter. Since syntax trees are processed in parallel, this rewriter must be safe to call concurrently from multiple threads.

CancellationToken cancellationToken

A CancellationToken to cancel the operation.

Returns
Type Description
Task
Remarks

This method processes all syntax trees in the compilation in parallel and automatically updates the Compilation property. Use this when you need to transform code throughout the entire compilation, not just aspect targets.

RewriteSyntaxTreesAsync(Func<SyntaxNode, CSharpSyntaxRewriter>, CancellationToken)

Rewrites all syntax trees in the compilation using a factory that creates a CSharpSyntaxRewriter per syntax tree.

Declaration
public Task RewriteSyntaxTreesAsync(Func<SyntaxNode, CSharpSyntaxRewriter> rewriterFactory, CancellationToken cancellationToken = default)
Parameters
Type Name Description
Func<SyntaxNode, CSharpSyntaxRewriter> rewriterFactory

A delegate that creates a CSharpSyntaxRewriter given the root SyntaxNode of a syntax tree. Called once for each SyntaxTree in the compilation. If the delegate returns the same rewriter instance for multiple trees, that instance must be thread-safe.

CancellationToken cancellationToken

A CancellationToken to cancel the operation.

Returns
Type Description
Task
Remarks

This method processes all syntax trees in the compilation in parallel and automatically updates the Compilation property.

Use this overload when each syntax tree requires its own rewriter state. For a shared rewriter, use RewriteSyntaxTreesAsync(CSharpSyntaxRewriter, CancellationToken).