creating-a-source-generator-part-1-creating-an-incremental-source-generator/ #24
Replies: 9 comments 10 replies
-
Hi there. One of the incremental source generator api authors here. If possible, if recommend adding a note (or followup post) mentioning that your can eliminate several steps (and have them run much more efficiently) if you use this new api: https://github.com/dotnet/roslyn/blob/7d64f6edcc0ee5fe4399a7966e1a6b2bd3ef9005/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithMetadataName.cs#L78 Thanks very much! |
Beta Was this translation helpful? Give feedback.
-
Hi Andrew, Thanks a lot for this tutorial. It helped me a lot. I do have one question regarding the pipeline you built. In the part where Syntax Provider is created ( How much of a problem would it be if I would move the complete filtering in your example, into the predicate and leave the transform to do only that, getting and transforming the data to what is needed further down the pipeline? Would you consider such a predicate inefficient? Talking strictly about your example. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
I’m unable to get this working on latest Rider version on MacOS. For me the source generator was able to generate code after I decorated my class with the marker attribute, even intellisense also worked. But when I try to run or compile the console app which consumes the source generator via project reference, it gives me error of “Cannot resolve the method which is auto generated |
Beta Was this translation helpful? Give feedback.
-
in section 3, what's the reason to define the enum as a source generated type instead of just a type in the assembly that can be referenced in downstream project? i feel like i'm losing simple compiler/type checking stuff i'm familiar with by defining the attribute in a string, but i'm guessing there's an advantage to doing it that way. |
Beta Was this translation helpful? Give feedback.
-
Hi Andrew: First of all, thank you for these articles. They helped me to design my own source generator for a couple of parts of my system that heavily relied on Reflection. I've seen improvements of up to ~90x in speed at some places. The "simple" generator (a single generator lib project) worked just fine, but since I splitted my generator into a few libraries to share code between 3 different generators, things went south and I've been unable to get it working again. My project configuration is almost identical to yours, but VS2022 fails to generate any code nor compile (my generated classes are not being found). I've been reading online and tried a few things. Most notably, I've been through some of the things mentioned here: Added what they mention in Pain Point 3 and now my generator seems to create my classes (at least VS2022 compiles my project) but can't see any generated code in the IDE (as with the "simple" generator). I'm not using the generator as a Nuget package but as a project reference for now. As I said before, the "simple" case being just one project, worked just fine, but when I separated the generator into different projects and started adding references between them, it stopped working. Did you experience any of this? Do you know where to look at? Thank you in advance! Andy |
Beta Was this translation helpful? Give feedback.
-
Jason Bock commented on Disqus at December 09 2021, 05:59 In the first method in the pipeline, could you search for AttributeSyntax nodes that have the name "EnumExtensions" or "EnumExtensionsAttribute"? Then you can find the enum it's associated with later. But then you know in your first pass that you have what you're looking for. Great article BTW :) |
Beta Was this translation helpful? Give feedback.
-
Rich Armstrong commented on Disqus at June 14 2022, 04:12 From step 6, Implementing the pipeline stages:
I'm guessing there were once plans for this little const, but they never got realized? |
Beta Was this translation helpful? Give feedback.
-
Raymond Osterbrink commented on Disqus at June 22 2022, 04:40 This tutorial is brilliant and was helping me so much in understanding the concept of the IIncrementalGenerator but still, there is one major question I could not answer: is it possible to access multiple projects inside my solution by the Generator, e.g. getting triggerd by changes in a shared Library and creating different code in the UI-project and the Backend-Project based on the changes? |
Beta Was this translation helpful? Give feedback.
-
Nice post , I always read it again to improve my generators I will change my generators to use global namespaces and I got a point here what is the best practice to generics and nullable? global::System.Threading.Tasks.Task<global::System.String?> x = null;
global::System.Threading.Tasks.Task<string?> x2 = null;
global::System.Threading.Tasks.Task<global::System.Nullable<global::System.String>> x3 = null; |
Beta Was this translation helpful? Give feedback.
-
creating-a-source-generator-part-1-creating-an-incremental-source-generator/
In this post I walk through how to create a practical .NET 6 incremental source generator: an enum extensions class with a fast ToString() implementation
https://andrewlock.net/creating-a-source-generator-part-1-creating-an-incremental-source-generator/
Beta Was this translation helpful? Give feedback.
All reactions