Last active
October 3, 2024 12:27
-
-
Save DragoQCC/d798553e39b946bd8e126dbb0056a52a to your computer and use it in GitHub Desktop.
SourceGen Blog1, value Provider example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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