Find all methods with an attribute using GeneratorExecutionContext
Im kind of new with source generators and Im trying to find all the methods marked with my attribute but can't approach, it would love if there was way to be guided by it
52 Replies
I don't know if what I want to do is possible or not but I assume it is
I want to find all classes that inherit from an abstract class and have a method decorated with my attribute
was kind of curious if this
gets me all classes that inherit from NetworkBehaviour
Rider warns me about it
Yeah, this isn't the way to go about it
You have a couple of misconceptions, and a couple of major issues in that snippet π
First: do not iterate all syntax trees
That will have terrible performance
Do I need to get root first?
No, abandon this approach altogether
Oh...
You're completely on the wrong track π
Are you following some tutorial?
Im trying to generate code for methods with an attribute but not sure how to do that
I followed some sample code
but it was doing something completely different
Where did that sample code come from?
Oh this isn't sample code from a tutorial
I was trying to figure it out myself
Oh
but was talking about previously
I would suggest looking at some samples π
Oh okay, any recommendations in mind? I'll try to look for more.
First and foremost: use incremental generators, not V1 generators
Second: if you can, use the https://sourceroslyn.io/#Microsoft.CodeAnalysis/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithMetadataName.cs,78 API that's going to be part of 2022.4
Oh okay never knew there was a difference
It will make what you're trying to do significantly simpler
using foreach?
No
It's an entirely different strategy of making a generator
Oh okay will look into it
Instead of implementing ISourceGenerator, you should implement IIncrementalGenerator
Oh got it
but kind of confused about the difference
but I guess I'll find out
Then, you use the various providers on the
context
to create a pipeline
ISourceGenerator was V1. IIncrementalGenerator is V2Oh interesting
A pipeline is like a LINQ query
so I guess I won't need ISourceGenerator anymore?
To use the above api, you'd do something like this:
Sorry noob question but how can I check which version of Microsoft.CodeAnalysis.Analyzer nuget packet this is in?
I can't find the declaration in the source code of the nuget version I got
It's in 4.4.0
Ie, latest prerelease
ah
That's why I said use it if you can, but I understand if you can't
Since it's not released yet
But the API is specifically designed for this case and will make your code ~10-100 orders of magnitude more performant (no exaggeration)
Oh wow.. okay I'll give it a shot
Would I be able to target it with .netstandard 2.0 or at least .net 4.0?
You must target standard2.0 in your generator
Oh okay that works for me then, also got another noob question, what's usually the best way to add the preview of the package to my project?
Ive never done it before
Well, I know how to do it in VS
Not sure about rider
since I dont think I can get it from nuget atm
You can get it from nuget
Oh how would it be done in vs usually?
Oh okay
There's a checkbox for allow prerelease
Microsoft.CodeAnalysis.CSharp 4.4.0-1.final
.NET Compiler Platform ("Roslyn") support for C#, Microsoft.CodeAnalysis.CSharp.dll.
More details at https://aka.ms/roslyn-packages This package was built from the source at https://github.com/dotnet/roslyn/commit/ebbf56c257fb4d3128d3487ef525d92e9f94b412.
More details at https://aka.ms/roslyn-packages This package was built from the source at https://github.com/dotnet/roslyn/commit/ebbf56c257fb4d3128d3487ef525d92e9f94b412.
Oh okay got it. :D
Right now I'm having one issue where even tho I got the right nuget package installed it seems the method doesn't exist
so I'm trying to find where it's declared and Im relatively new with rider so need to look up some shortcuts
oh nvm for some reason I had the old wrong version reference in my csproj
Can I ask you what you mean by transform here?
(context, token) => /* Transform into the next step of your pipeline */)
Extract the data you need from the syntax and put it in a model, likely a simple
record
typeOh right
so I'm planning on sending all the parameter into a buffer
so I guess I'd extract the parameters of the method
and their type
did I understand that correctly?
You need to extract all the data you need to generate code
got it
BTW, #roslyn is a channel π
Oh I should check that out. :D
is it better to ask there with roslyn related questions?
Yes
got it will do from now on :D
rider also has a checkbox for preview versionsβ¦ even a checkbox for unlisted packages if you like to live dangerously π