C
C#

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 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
IIcetrack9/1/2022
Rider warns me about it
F333fred9/1/2022
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
IIcetrack9/1/2022
Do I need to get root first?
F333fred9/1/2022
No, abandon this approach altogether
IIcetrack9/1/2022
Oh...
F333fred9/1/2022
You're completely on the wrong track ๐Ÿ™‚ 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 I followed some sample code but it was doing something completely different
F333fred9/1/2022
Where did that sample code come from?
IIcetrack9/1/2022
Oh this isn't sample code from a tutorial I was trying to figure it out myself
F333fred9/1/2022
Oh
IIcetrack9/1/2022
but was talking about previously
F333fred9/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.
F333fred9/1/2022
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
IIcetrack9/1/2022
Oh okay never knew there was a difference
F333fred9/1/2022
It will make what you're trying to do significantly simpler
IIcetrack9/1/2022
using foreach?
F333fred9/1/2022
No It's an entirely different strategy of making a generator
IIcetrack9/1/2022
Oh okay will look into it
F333fred9/1/2022
Instead of implementing ISourceGenerator, you should implement IIncrementalGenerator
IIcetrack9/1/2022
Oh got it but kind of confused about the difference but I guess I'll find out
F333fred9/1/2022
Then, you use the various providers on the context to create a pipeline ISourceGenerator was V1. IIncrementalGenerator is V2
IIcetrack9/1/2022
Oh interesting
F333fred9/1/2022
A pipeline is like a LINQ query
IIcetrack9/1/2022
so I guess I won't need ISourceGenerator anymore?
F333fred9/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
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
F333fred9/1/2022
It's in 4.4.0 Ie, latest prerelease
IIcetrack9/1/2022
ah
F333fred9/1/2022
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)
IIcetrack9/1/2022
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?
F333fred9/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? Ive never done it before
F333fred9/1/2022
Well, I know how to do it in VS Not sure about rider
IIcetrack9/1/2022
since I dont think I can get it from nuget atm
F333fred9/1/2022
You can get it from nuget
IIcetrack9/1/2022
Oh how would it be done in vs usually? Oh okay
F333fred9/1/2022
There's a checkbox for allow prerelease
F333fred9/1/2022
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.
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 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 */)
F333fred9/1/2022
Extract the data you need from the syntax and put it in a model, likely a simple record type
IIcetrack9/1/2022
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
F333fred9/1/2022
You need to extract all the data you need to generate code
IIcetrack9/1/2022
got it
F333fred9/1/2022
BTW, #roslyn is a channel ๐Ÿ™‚
IIcetrack9/1/2022
Oh I should check that out. :D is it better to ask there with roslyn related questions?
F333fred9/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 ๐Ÿ˜‚

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Color brush from App.xaml`App.xaml` has a `ResourceDictionary` that contains a `SolidColorBrush`. How can I get that `SolidCThread.Sleep(int32) not working? Send Help [Answered]```cs private void BtnUpgrade_Click(object sender, EventArgs e) { if (IsEqScrolldate time excisesPlease help me do my code Note : Iโ€™m new c# learningDependency injection - transient services inside a singleton?I'm in Monogame (not my choice) and using DI to resolve various services I need, including those thadotnet restore to ignore packagesI have several projects in my solution: 1. Web API 2. Worker service 3. Application layer and many oDapper map to multiple DictionariesI'm using Dapper and .NET core 6 and am trying to map an object with multiple dictionaries in one sqreceiving, saving and returning UTC DateTimeHi. In Api I get a DateTime from the client in UTC format - with "Z" at the end of the string. I wanstackwhat class would i use for a stack? eg ```cs stack.push(1); stack.push(2); stack.top() == 2; stackHow to access wanted property of a class instance? [Answered]So i have a class in which i generate it's instances with properties A B C D and i save them to a fiCan someone help me i got this error? [Answered]Error on status and ref statusCapturing keystroke signalsHiya, I am looking for a way to catch the SIGTSTP signal triggered by CTRL+Z in a net6.0 console appParsing JSONI'm struggling to parse and iterate over a JSON array of "key/object" elements. For instance, somethAccidentally merged untested code in the stable branch, can't find the merge to revert [Answered]Title explains it. https://github.com/TheBoxyBear/charttools I accidentally merged beta into stable EF Core + SQLite order by decimal value [Answered]Hey I tried to sort a query via a decimal column but I get this exception: ``System.NotSupportedExceRider suggestion - creating a property for a DateTime field will introduce struct copyingI encountered this warning from Rider recently when refactoring some old code. I had a class with seCan someone explain this for me```cs namespace CSharp { internal class Program { static void Main(string[] args) ``HashCode randomly changing on structures.I've never in my career of coding have seen the behavior I'm about to describe so I'm hoping to learLinq Where JsonSerialization not workingHey, is there a way to get the following running: ```cs dbSet.Set<Table>().Where(x => JsonSerializerMicrosoft's DI Hierarchically controlled lifetime manager? [Answered]With Microsoft's DI, what's the equivalent of hierarchically controlled lifetime manager? The situatDisable bootstrap multiselect options after selectingHi. I have a bootstrap `<select multiple/>`. When one is selected, I want to disable all other selec