Using Source Generators for Fun (and Maybe Profit)
Вставка
- Опубліковано 21 жов 2024
- One of the features added in C# 9 is called "source generators", enabling a developer to create new code at compile time. This can be used for a myriad of cases, such as optimization, automating repetitive code, and dynamic API creation. In this session, I'll cover how source generators work and demonstrate a number of implementations.
aka.ms/dotNETC... 👨💻 Slides & code: aka.ms/dotnetc...
🎉 .NET 6 Release Blog: devblogs.micro...
⬇ Download .NET 6: aka.ms/dotNETC...
🙋♀️🙋♂️ Get your questions answered on the Microsoft Q&A for .NET: aka.ms/dotnet-qa
💭 Join the .NET Tech Community Forums: aka.ms/dotnet/...
🏫 Learn C#, F#, and .NET with free self-guided learning from Microsoft Learn: aka.ms/learndo...
#dotnet6 #dotnetconf #dotnet
One potential use for Source Generators is to automatically create the proxies for the apis you are consuming, basically create an Addional File with the Open Api url and generate the classes.
Moreover, with source generators I think you could even create a whole system (Data Access, Api Endpoints, Services, and even UI automatically, based on pre-established patterns
I love the Source Generators in the MVVM Toolkit.
Thanks for the demo! I learned a lot from you actually :). I have a question, if we specify the consuming project to spit out the files under Analyzer and also used CompilerGeneratedFilesOutputPath to generate under a folder to physically be shown to other developers, how can I get rid of the generated files under Analyzers because it causes an ambiguous issue for the compiler to know from where it should call a function in my code?
Where's the source code for the presentation?
Honestly, I'm just waiting for someone to make an easy to follow tutorial on the source generator with a "hello world" example because as exciting as it may seem, this video is very confusing and definitely too complex to follow, it seems like an incredibly over-engineered piece of code.
I think if u need ask that question its prob not the right topic we all want to create our own lanagues but like me i just admire their smarts
What is confusing? Would be happy to explain anything in more detail.
@@JasonBock 11:05 is probably where @Zoran starts to get overwhelmed - especially where MapGenerator class implements GenerateMapping with a very complex looking return type and formal parameters. I'm guessing we can just go ahead and create this class, get C# to implement the interface and then start out simple, inspecting what we've got to play with via Intellisense and a bit of debugging?
Also, can I clarify:
This is a *class library* project?
We need the Microsoft.CodeAnalysis projects references - is this what contains all the types such as ISourceGenerator IMapGenerator?
Isn't the background picture the Rajská Zahrada tube station?
Hi thanks for this video, it helps me a lot. You talk about a MS source generator cookbook, is it public and can it be downloaded?
Thank you.
The learning curve…
....is there. 100% agree with that. The Compiler API is not a trivial API to understand and use. That said, I believe that if you invest time learning it, you have a powerful tool available to you to write source generators, analyzers, code fixes, refactorings, and other .NET tools.
@@JasonBock Could you give us some links to the specific documentation ?
Way too complex to be usable !
Not sure what is unusable here. A consumer of a source generator - specifically with the object mapping example I showed - has to add one attribute to their code per mapping. That's it. Then you get an extension method generated for you. Authoring a source generator is not trivial - I would agree with that point - but the consumer shouldn't see any of that.
17:18 Pause
6:38 This is the reason why source generators are not popular.
Not sure what the specific issue here.
@@Flynnor Possibly. Though, then you're tied to that specific IDE (assuming it's done for that IDE's API).