C
C#β€’3y ago
Icetrack

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
Icetrack
IcetrackOPβ€’3y ago
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
var classInheritingFromBaseClass = context.Compilation.SyntaxTrees
.Where(classes => classes is NetworkBehaviour);
var classInheritingFromBaseClass = context.Compilation.SyntaxTrees
.Where(classes => classes is NetworkBehaviour);
gets me all classes that inherit from NetworkBehaviour
Icetrack
IcetrackOPβ€’3y ago
Rider warns me about it
333fred
333fredβ€’3y ago
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
Icetrack
IcetrackOPβ€’3y ago
Do I need to get root first?
333fred
333fredβ€’3y ago
No, abandon this approach altogether
Icetrack
IcetrackOPβ€’3y ago
Oh...
333fred
333fredβ€’3y ago
You're completely on the wrong track πŸ™‚ Are you following some tutorial?
Icetrack
IcetrackOPβ€’3y ago
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
333fred
333fredβ€’3y ago
Where did that sample code come from?
Icetrack
IcetrackOPβ€’3y ago
Oh this isn't sample code from a tutorial I was trying to figure it out myself
333fred
333fredβ€’3y ago
Oh
Icetrack
IcetrackOPβ€’3y ago
but was talking about previously
333fred
333fredβ€’3y ago
I would suggest looking at some samples πŸ™‚
Icetrack
IcetrackOPβ€’3y ago
Oh okay, any recommendations in mind? I'll try to look for more.
333fred
333fredβ€’3y ago
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
Icetrack
IcetrackOPβ€’3y ago
Oh okay never knew there was a difference
333fred
333fredβ€’3y ago
It will make what you're trying to do significantly simpler
Icetrack
IcetrackOPβ€’3y ago
using foreach?
333fred
333fredβ€’3y ago
No It's an entirely different strategy of making a generator
Icetrack
IcetrackOPβ€’3y ago
Oh okay will look into it
333fred
333fredβ€’3y ago
Instead of implementing ISourceGenerator, you should implement IIncrementalGenerator
Icetrack
IcetrackOPβ€’3y ago
Oh got it but kind of confused about the difference but I guess I'll find out
333fred
333fredβ€’3y ago
Then, you use the various providers on the context to create a pipeline ISourceGenerator was V1. IIncrementalGenerator is V2
Icetrack
IcetrackOPβ€’3y ago
Oh interesting
333fred
333fredβ€’3y ago
A pipeline is like a LINQ query
Icetrack
IcetrackOPβ€’3y ago
so I guess I won't need ISourceGenerator anymore?
333fred
333fredβ€’3y ago
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
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
Icetrack
IcetrackOPβ€’3y ago
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
333fred
333fredβ€’3y ago
It's in 4.4.0 Ie, latest prerelease
Icetrack
IcetrackOPβ€’3y ago
ah
333fred
333fredβ€’3y ago
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)
Icetrack
IcetrackOPβ€’3y ago
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?
333fred
333fredβ€’3y ago
You must target standard2.0 in your generator
Icetrack
IcetrackOPβ€’3y ago
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
333fred
333fredβ€’3y ago
Well, I know how to do it in VS Not sure about rider
Icetrack
IcetrackOPβ€’3y ago
since I dont think I can get it from nuget atm
333fred
333fredβ€’3y ago
You can get it from nuget
Icetrack
IcetrackOPβ€’3y ago
Oh how would it be done in vs usually? Oh okay
333fred
333fredβ€’3y ago
There's a checkbox for allow prerelease
333fred
333fredβ€’3y ago
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.
Icetrack
IcetrackOPβ€’3y ago
Oh okay got it. :D
Icetrack
IcetrackOPβ€’3y ago
Right now I'm having one issue where even tho I got the right nuget package installed it seems the method doesn't exist
Icetrack
IcetrackOPβ€’3y ago
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 */)
333fred
333fredβ€’3y ago
Extract the data you need from the syntax and put it in a model, likely a simple record type
Icetrack
IcetrackOPβ€’3y ago
Oh 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? catsweat
333fred
333fredβ€’3y ago
You need to extract all the data you need to generate code
Icetrack
IcetrackOPβ€’3y ago
got it
333fred
333fredβ€’3y ago
BTW, #roslyn is a channel πŸ™‚
Icetrack
IcetrackOPβ€’3y ago
Oh I should check that out. :D is it better to ask there with roslyn related questions?
333fred
333fredβ€’3y ago
Yes
Icetrack
IcetrackOPβ€’3y ago
got it will do from now on :D
matkoch
matkochβ€’3y ago
rider also has a checkbox for preview versions… even a checkbox for unlisted packages if you like to live dangerously πŸ˜‚
Want results from more Discord servers?
Add your server