C
Join ServerC#
help
Find all methods with an attribute using GeneratorExecutionContext
IIcetrack9/1/2022
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
IIcetrack9/1/2022
I don't know if what I want to do is possible or not but I assume it is
IIcetrack9/1/2022
I want to find all classes that inherit from an abstract class and have a method decorated with my attribute
IIcetrack9/1/2022
was kind of curious if this
gets me all classes that inherit from NetworkBehaviour
var classInheritingFromBaseClass = context.Compilation.SyntaxTrees
.Where(classes => classes is NetworkBehaviour);
gets me all classes that inherit from NetworkBehaviour
IIcetrack9/1/2022
Rider warns me about it

OOrannis9/1/2022
Yeah, this isn't the way to go about it
OOrannis9/1/2022
You have a couple of misconceptions, and a couple of major issues in that snippet π
OOrannis9/1/2022
First: do not iterate all syntax trees
OOrannis9/1/2022
That will have terrible performance
IIcetrack9/1/2022
Do I need to get root first?
OOrannis9/1/2022
No, abandon this approach altogether
IIcetrack9/1/2022
Oh...
OOrannis9/1/2022
You're completely on the wrong track π
OOrannis9/1/2022
Are you following some tutorial?
IIcetrack9/1/2022
Im trying to generate code for methods with an attribute but not sure how to do that
IIcetrack9/1/2022
I followed some sample code
IIcetrack9/1/2022
but it was doing something completely different
OOrannis9/1/2022
Where did that sample code come from?
IIcetrack9/1/2022
Oh this isn't sample code from a tutorial
IIcetrack9/1/2022
I was trying to figure it out myself
OOrannis9/1/2022
Oh
IIcetrack9/1/2022
but was talking about previously
OOrannis9/1/2022
I would suggest looking at some samples π
IIcetrack9/1/2022
Oh okay, any recommendations in mind? I'll try to look for more.
OOrannis9/1/2022
First and foremost: use incremental generators, not V1 generators
OOrannis9/1/2022
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
IIcetrack9/1/2022
Oh okay never knew there was a difference
OOrannis9/1/2022
It will make what you're trying to do significantly simpler
IIcetrack9/1/2022
using foreach?
OOrannis9/1/2022
No
OOrannis9/1/2022
It's an entirely different strategy of making a generator
IIcetrack9/1/2022
Oh okay will look into it
OOrannis9/1/2022
Instead of implementing ISourceGenerator, you should implement IIncrementalGenerator
IIcetrack9/1/2022
Oh got it
IIcetrack9/1/2022
but kind of confused about the difference
IIcetrack9/1/2022
but I guess I'll find out
OOrannis9/1/2022
Then, you use the various providers on the
context
to create a pipelineOOrannis9/1/2022
ISourceGenerator was V1. IIncrementalGenerator is V2
IIcetrack9/1/2022
Oh interesting
OOrannis9/1/2022
A pipeline is like a LINQ query
IIcetrack9/1/2022
so I guess I won't need ISourceGenerator anymore?
OOrannis9/1/2022
To use the above api, you'd do something like this:
context.SyntaxProvider.ForAttributeWithMetadataName("Fully.Qualified.Name", (node, token) => /* Syntax checks for whether you want to look at this method */, (context, token) => /* Transform into the next step of your pipeline */) ... // More steps of your pipeline
IIcetrack9/1/2022
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
I can't find the declaration in the source code of the nuget version I got


OOrannis9/1/2022
It's in 4.4.0
OOrannis9/1/2022
Ie, latest prerelease
IIcetrack9/1/2022
ah
OOrannis9/1/2022
That's why I said use it if you can, but I understand if you can't
OOrannis9/1/2022
Since it's not released yet
OOrannis9/1/2022
But the API is specifically designed for this case and will make your code ~10-100 orders of magnitude more performant (no exaggeration)
IIcetrack9/1/2022
Oh wow.. okay I'll give it a shot
IIcetrack9/1/2022
Would I be able to target it with .netstandard 2.0 or at least .net 4.0?
OOrannis9/1/2022
You must target standard2.0 in your generator
IIcetrack9/1/2022
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?
IIcetrack9/1/2022
Ive never done it before
OOrannis9/1/2022
Well, I know how to do it in VS
OOrannis9/1/2022
Not sure about rider
IIcetrack9/1/2022
since I dont think I can get it from nuget atm
OOrannis9/1/2022
You can get it from nuget
IIcetrack9/1/2022
Oh how would it be done in vs usually?
IIcetrack9/1/2022
Oh okay
OOrannis9/1/2022
There's a checkbox for allow prerelease
OOrannis9/1/2022
IIcetrack9/1/2022
Oh okay got it. :D
IIcetrack9/1/2022
Right now I'm having one issue where even tho I got the right nuget package installed it seems the method doesn't exist

IIcetrack9/1/2022
so I'm trying to find where it's declared and Im relatively new with rider so need to look up some shortcuts
IIcetrack9/1/2022
oh nvm for some reason I had the old wrong version reference in my csproj
IIcetrack9/1/2022
Can I ask you what you mean by transform here?
(context, token) => /* Transform into the next step of your pipeline */)
OOrannis9/1/2022
Extract the data you need from the syntax and put it in a model, likely a simple
record
typeIIcetrack9/1/2022
Oh right
IIcetrack9/1/2022
so I'm planning on sending all the parameter into a buffer
IIcetrack9/1/2022
so I guess I'd extract the parameters of the method
IIcetrack9/1/2022
and their type
IIcetrack9/1/2022
did I understand that correctly? 

OOrannis9/1/2022
You need to extract all the data you need to generate code
IIcetrack9/1/2022
got it
OOrannis9/1/2022
BTW, #roslyn is a channel π
IIcetrack9/1/2022
Oh I should check that out. :D
IIcetrack9/1/2022
is it better to ask there with roslyn related questions?
OOrannis9/1/2022
Yes
IIcetrack9/1/2022
got it will do from now on :D
Mmatkoch9/2/2022
rider also has a checkbox for preview versionsβ¦ even a checkbox for unlisted packages if you like to live dangerously π