C# Source Generators - Write Code that Writes Code

Поділитися
Вставка
  • Опубліковано 21 жов 2024
  • With C# 9 there is finally an officially supported mechanism for generating source code into your .NET projects as part of the compiler pipeline. Lets run through how they work, some of the pros and cons, and play around with ideas to get your mind racing with the possibilities.
    Get your questions answered on the Microsoft Q&A for .NET - aka.ms/dotnet-qa​
    Learn .NET with free self-guided learning from Microsoft Learn: aka.ms/learndotnet

КОМЕНТАРІ • 39

  • @CodingHaribo
    @CodingHaribo 3 роки тому +5

    Great demo! I foresee some fantastic use cases for a source-generator. Can't wait to try them out

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

    Excellent presentation and demonstration! Really cool stuff.

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

    T4 vs source generators:
    T4 can't access your syntax tree. Access to the syntax tree *the primary* advantage of source generators. The two source generator examples that MS first blogged: CSV and "moustache" gen, were entirely possible to implement without the Source Generator feature, using only MSBuild. T4 is basically the same thing.
    Source Generators can access anything about your project. But it is up to you to communicate that information to the SG via AdditionalFiles. You can have an MS build task that runs before compilation that ships whatever information is needed from the project down to the SG.
    You can implement T4 via Source generators, but you can't (necessarily) implement the capabilities of source generators via T4.

  • @xiety666
    @xiety666 3 роки тому +8

    I think one could use t4 Runtime Text Template to make it easier to develop Source Generators, instead of using StringBuilders

  • @tonytins
    @tonytins 3 роки тому +1

    Source generators is very useful when combined with C# 9's new record types.

  • @Crossbow123
    @Crossbow123 3 роки тому +1

    Love the legos the guy has in the background :)

  • @adrianyankov2209
    @adrianyankov2209 3 роки тому

    Really cool technology. Great video! CODE GENERATORS RULE!

  • @Albileon
    @Albileon 3 роки тому +1

    Amazing demo! Can't wait to play with this :)

  • @willinton06
    @willinton06 3 роки тому

    Blazor will greatly benefit from this

  • @hydri8248
    @hydri8248 3 роки тому

    Most of that went right over my head.

  • @S3Kglitches
    @S3Kglitches 3 роки тому

    what if Enum is built on long (EnumType : long) - e.g. for some complicated flags type? You are assuming only ints or subsets are used

  • @spydacarnage
    @spydacarnage 3 роки тому

    Which preview version of Visual Studio are you using to have the live updates? I'm using 16.9 preview 1 and I still have to quit VS and reload to see the changes...

  • @williamsalazar2624
    @williamsalazar2624 3 роки тому

    David please can you update your course of T4 in Pluralsight

  • @j2csharp
    @j2csharp 3 роки тому

    I haven't watched the full video, but I'm wondering if there's any security concerns here. Would code be able to slip in undetected, for example? hm.

  • @JeremyCaney
    @JeremyCaney 3 роки тому +1

    Are there examples or guidance available for how to connect to a database or external API in order to model generated classes off of either data or schema information, while maintaining secrets via e.g. a configuration provider? I’m assuming this is possible by injecting the dependencies-such as a connection string, API key, or even a data access library-to the source generator project, but haven’t dived into the architecture enough to confirm what an appropriate composition root would be. Or would this data instead need to be first persisted as a static dependency-such as a local CSV or JSON file-to ensure access at compile time? (Obviously, any such data would also need to be aggressively cached for the sake of performance, instead of querying the database each time the generator is triggered.)

    • @DavidWengier
      @DavidWengier 3 роки тому +3

      The compiler constructs the instance of your generator and its lifetime is completely out of your hands, and there is no DI capabilities. Reading from a database or web service as part of compilation is a very bad idea for performance, so I doubt any feature request will go far, but you can always ask!
      The way to get data into the system to inform the generation is using AdditionalFiles so you would have to write some msbuild tasks for get that data, and output the right items, in order to use them in a generator.

  • @MrXzxzxc
    @MrXzxzxc 3 роки тому

    Thanks for the video. I have a question, do you use some extension to have this pretty comparison operator signs ≥ ≤ ≠?

    • @developpa1519
      @developpa1519 3 роки тому

      They are called ligatures. You can use any Ligature supporting font for that. I think he is using Fira Code font.

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

    Can I use it to generate files that should no be compiled by cs e.g. a typescript file?

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

      No, only C# source files can be generated at the moment.

  • @Courserasrikanthdrk
    @Courserasrikanthdrk 3 роки тому

    very well information. thanks

  • @justnothing7080
    @justnothing7080 3 роки тому

    Looks like this is against reflection, metadata, algorithm,... by putting a lot of deadly simple code into automation. So as before you fear of writing a lot of code like that manually. You use reflection, metadata, algorithm,.... to minimize the work of typing and the amount of repetitive code or garbages in your project. With this new feature you can automate the manual work 😄 and still have best performance of the final built code. It's still a feature and we don't have to use all the features while developing.

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

    What is the link to the EnumValidator source code?

    • @FunWithBits
      @FunWithBits 3 роки тому +1

      goto sourcegen.dev
      then select load sample at the top

    • @Pezsmapatkany
      @Pezsmapatkany 3 роки тому +1

      @@FunWithBits Thanks :)

  • @burakgumus7902
    @burakgumus7902 3 роки тому

    Seems great.

  • @Proculopsis
    @Proculopsis 3 роки тому +3

    💓 11:58 Useful links:
    ▶ github.com/DavidWengier/SourceGeneratorTemplate
    ▶ github.com/chsienki/kittitas
    ▶ sourcegen.dev/
    Thanks very much for an excellent presentation.

  • @FunWithBits
    @FunWithBits 3 роки тому

    Links for this video... (see 13:44)
    github.com/davidwengier/sourcegeneratortemplate
    github.com/chsienki/kittitas
    sourcegen.dev
    roslynquoter.azurewebsites.net

  • @steveariel
    @steveariel 3 роки тому

    links from slides
    github.com/davidwengier/sourcegeneratortemplate
    github.com/chsienki/kittitas
    sourcegen.dev/

  •  3 роки тому +3

    looks complicated. I should use inside something.

  • @phill13able
    @phill13able 3 роки тому

    So I can't produce viruses anymore?...bummer

  • @DgreatRyan21
    @DgreatRyan21 3 роки тому +1

    Do I need to be bald to be good at writing code?

    • @jemsworld
      @jemsworld 3 роки тому

      You are soo funny, surely you will be bald best programmer one day 👍

  • @Kampouse
    @Kampouse 3 роки тому +1

    btw this not new... many lower language are compiled down to c or web base compiled to js .... nothing new here ;)

    • @justnothing7080
      @justnothing7080 3 роки тому +4

      What you mentioned is "transpiling". Here is something like "compiling injection". They are different.

  • @ghrARASH
    @ghrARASH 3 роки тому

    Too much Talk. Show me the Code.

  • @ExSpoonman
    @ExSpoonman 3 роки тому

    Bruh. This is the most jankiest code I've seen written in the longest time. Hole-lee-shit is that entire GitHub project a massive turd. Hopefully this feature won't get tossed in the bin by Microsoft and we can enjoy the functionality sometime within the next fucking decade. Do us all a favor and stop hyping shit until IT WORKS. Jesus, this is how we got the clusterfuck that is Python.