Skip to content

Instantly share code, notes, and snippets.

@DragoQCC
Last active October 3, 2024 12:27
Show Gist options
  • Save DragoQCC/d798553e39b946bd8e126dbb0056a52a to your computer and use it in GitHub Desktop.
Save DragoQCC/d798553e39b946bd8e126dbb0056a52a to your computer and use it in GitHub Desktop.
SourceGen Blog1, value Provider example
IncrementalValuesProvider<ClassDeclarationSyntax> calculatorClassesProvider = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (SyntaxNode node, CancellationToken cancelToken) =>
{
//the predicate should be super lightweight to filter out items that are not of interest quickly
return node is ClassDeclarationSyntax classDeclaration && classDeclaration.Identifier.ToString() == "Calculator";
},
transform: (GeneratorSyntaxContext ctx, CancellationToken cancelToken) =>
{
//the transform is called only when the predicate returns true, so it can do a bit more heavyweight work but should mainly be about getting the data we want to work with later
var classDeclaration = (ClassDeclarationSyntax)ctx.Node;
return classDeclaration;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment