C
C#•3y ago
nobody

Source Generator only annotate method instead of class

Is it possible to annotate only a method with a custom attribute instead of the class itself?
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
switch (syntaxNode)
{
case ClassDeclarationSyntax classDeclarationSyntax when
classDeclarationSyntax.HasAttribute<TAttribute>():
//Process...
break;
case MethodDeclarationSyntax methodDeclarationSyntax when
methodDeclarationSyntax.HasAttribute<TAttribute>():
//Process...
break;
}
}
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
switch (syntaxNode)
{
case ClassDeclarationSyntax classDeclarationSyntax when
classDeclarationSyntax.HasAttribute<TAttribute>():
//Process...
break;
case MethodDeclarationSyntax methodDeclarationSyntax when
methodDeclarationSyntax.HasAttribute<TAttribute>():
//Process...
break;
}
}
HasAttribute is a custom method which simply checks for the attribute, for classes it works just fine - for method it does not.. Would that be the right way? Assuming that HasAttribute works fine for methods too
10 Replies
333fred
333fred•3y ago
Are you asking for how to only process methods annotated with an attribute?
nobody
nobodyOP•3y ago
yes I wanna look up for methods annotated with [CustomAttribute]
333fred
333fred•3y ago
Scrap everything you've done so far Use incremental source generators and ForAttributeWithMetadataName https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md covers incremental generators ForAttributeWithMetadataName is one of the methods on SyntaxValueProvider. The method is brand new and not directly covered in the above document, but is very similar to CreateSyntaxProvider, which is covered That method was designed for exactly this scenario, and will make it both significantly easier and significantly faster Literally 100x faster than trying to do this manually Also, #roslyn exists for asking questions about source generators and the like 🙂
nobody
nobodyOP•3y ago
Alright I'll give it a shot - thank you very very much for the quick explanation, I didn't even know about the existence of incremental source generators
333fred
333fred•3y ago
ISourceGenerator was the V1. It had some unfixable perf problems, so IIncrementalSourceGenerators were created As long as you're using VS 2022.3, you'll have access to ForAttributeWithMetadataName If you're in VS 2019, you will have to continue using ISourceGenerator But also, upgrade!
nobody
nobodyOP•3y ago
Ah got it, I've noticed that the code always gets regenerated even if nothing changed.. is some kind of memoization possible to implement?
333fred
333fred•3y ago
That would be one of the unsolvable perf problems in V1 🙂
nobody
nobodyOP•3y ago
I am using the latest VS2022, so thats fine :P
333fred
333fred•3y ago
IIncrementalSourceGenerator and ForAttributeWithMetadataName were designed specifically to make this fast and run as little as possible
nobody
nobodyOP•3y ago
That's awsome! Gotta love the source gens they make your life so much easier in larger apps
Want results from more Discord servers?
Add your server