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

КОМЕНТАРІ • 21

  • @PticostaricaGS
    @PticostaricaGS 2 роки тому +5

    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

  • @tea_otomo
    @tea_otomo 2 роки тому

    I love the Source Generators in the MVVM Toolkit.

  • @codearabawy
    @codearabawy 2 роки тому

    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?

  • @brunoccs
    @brunoccs 2 роки тому +3

    Where's the source code for the presentation?

  • @zoran123456
    @zoran123456 2 роки тому +24

    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.

    • @dotnetdevni
      @dotnetdevni 2 роки тому

      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

    • @JasonBock
      @JasonBock 2 роки тому +4

      What is confusing? Would be happy to explain anything in more detail.

    • @ChrisWalshZX
      @ChrisWalshZX 2 роки тому +5

      @@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?

  • @jaroslavruzicka5681
    @jaroslavruzicka5681 2 роки тому

    Isn't the background picture the Rajská Zahrada tube station?

  • @mcnets
    @mcnets 2 роки тому

    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?

  • @aminejadid2702
    @aminejadid2702 2 роки тому +1

    Thank you.

  • @queenofpain6483
    @queenofpain6483 2 роки тому +6

    The learning curve…

    • @JasonBock
      @JasonBock 2 роки тому +4

      ....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.

    • @MarioLopez-eu8tj
      @MarioLopez-eu8tj 2 роки тому +2

      @@JasonBock Could you give us some links to the specific documentation ?

  • @geoffw.5003
    @geoffw.5003 2 роки тому +5

    Way too complex to be usable !

    • @JasonBock
      @JasonBock 2 роки тому +2

      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.

  • @ivandrofly
    @ivandrofly Рік тому

    17:18 Pause

  • @IvanNochnoy
    @IvanNochnoy 2 роки тому +2

    6:38 This is the reason why source generators are not popular.

    • @JasonBock
      @JasonBock 2 роки тому

      Not sure what the specific issue here.

    • @JasonBock
      @JasonBock 2 роки тому

      @@Flynnor Possibly. Though, then you're tied to that specific IDE (assuming it's done for that IDE's API).