Delen via


InitializationContext.RegisterForSyntaxNotifications Method

Definition

Register a SyntaxReceiverCreator for this generator, which can be used to create an instance of an ISyntaxReceiver.

public void RegisterForSyntaxNotifications (Microsoft.CodeAnalysis.SyntaxReceiverCreator receiverCreator);
member this.RegisterForSyntaxNotifications : Microsoft.CodeAnalysis.SyntaxReceiverCreator -> unit
Public Sub RegisterForSyntaxNotifications (receiverCreator As SyntaxReceiverCreator)

Parameters

receiverCreator
SyntaxReceiverCreator

A SyntaxReceiverCreator that can be invoked to create an instance of ISyntaxReceiver

Remarks

This method allows generators to be 'syntax aware'. Before each generation the receiverCreator will be invoked to create an instance of ISyntaxReceiver. This receiver will have its OnVisitSyntaxNode(SyntaxNode) invoked for each syntax node in the compilation, allowing the receiver to build up information about the compilation before generation occurs.

During Execute(SourceGeneratorContext) the generator can obtain the ISyntaxReceiver instance that was created by accessing the SyntaxReceiver property. Any information that was collected by the receiver can be used to generate the final output.

A new instance of ISyntaxReceiver is created per-generation, meaning there is no need to manage the lifetime of the receiver or its contents.

Applies to