"Generator Class Syntax Receiver Example"

class SyntaxPrinter : ISyntaxReceiver {
	readonly FileStream _fs;
	readonly TextWriter _tw;

	public SyntaxPrinter() {
		_fs = File.OpenWrite(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test.txt"));
		_tw = new StreamWriter(_fs);
	}

	public void OnVisitSyntaxNode(SyntaxNode syntaxNode) {
		_tw.WriteLine($"Node received: {syntaxNode.Kind()} {syntaxNode}");
		_fs.Flush();
	}
}

[Generator]
public class MySourceGenerator : ISourceGenerator {
	public void Execute(GeneratorExecutionContext context) {
		// TODO
	}

	public void Initialize(GeneratorInitializationContext context) {
		context.RegisterForSyntaxNotifications(() => new SyntaxPrinter());
	}
}


Code snippet taken from "Two Decades of C#: A Reference - C# 9".