C# Source Generators Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ค. 2024
  • Introduction to C# Source Generators, a comprehensive development guide covering the process of creating source generators in Visual Studio and Rider.
    Patreon 🤝 / raw_coding
    Courses 📚 learning.raw-coding.dev
    Shop 🛒 shop.raw-coding.dev
    Discord 💬 / discord
    Twitter 📣 / anton_t0shik
    Twitch 🎥 / raw_coding
    Source Code: / raw_coding
    Docs: docs.microsoft.com/en-us/dotn...
    Cookbook: github.com/dotnet/roslyn/blob...
    Roslyn Quoter: roslynquoter.azurewebsites.net/
    00:00 Introduction
    01:38 Brief
    05:43 Setup
    07:34 Diagnostics
    11:57 Reading Source
    17:33 Visual Studio Extension
    18:31 Syntax Receiver
    21:28 Output
    28:00 Read Implementation
    40:28 Process Implementation
    47:57 Non-static Function Example
    #csharp #dotnet #programming

ความคิดเห็น • 58

  • @RawCoding
    @RawCoding  ปีที่แล้ว +2

    Thank you to TwentyFourMinutes for teaching me about source generators.
    Please take a minute to star his PostgreSQL ORM Repo github.com/TwentyFourMinutes/Venflow

  • @josmitube
    @josmitube ปีที่แล้ว +2

    The most complicated 'Hello world!' toturial I've seen in my life. But I love it!

  • @solidsloth1
    @solidsloth1 ปีที่แล้ว +1

    This was super helpful. I was having a hard time getting started using the Microsoft Docs, but this has given me the confidence to really dig in. Just wish I'd found this 6 months ago. Thanks for the great video!

  • @evanboltsis
    @evanboltsis 2 ปีที่แล้ว

    Your channel is so underrated. Thanks for another great video.

  • @emmanueladebiyi2109
    @emmanueladebiyi2109 8 หลายเดือนก่อน

    Amazing tutorial. Thanks

  • @abyshekhar
    @abyshekhar 8 หลายเดือนก่อน

    Valuable information 🙏🏻

  • @jonasgranlund4427
    @jonasgranlund4427 2 ปีที่แล้ว +1

    Really good content. I look forward to look into this area more.

  • @alexiosplomaritis9713
    @alexiosplomaritis9713 2 ปีที่แล้ว

    Great content as always :)

  • @SharafMansour
    @SharafMansour 2 ปีที่แล้ว +2

    Wow awesome work! I needed this video ♥️

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว +1

      Glad you like it )

  • @brunogiovagnoli3022
    @brunogiovagnoli3022 2 ปีที่แล้ว

    I loved the thumbnail, thanks as always

  • @noggog5106
    @noggog5106 2 ปีที่แล้ว +6

    My preferred way of debugging source generators is via unit tests. Not only are they good to do to catch all the edge cases, as usual, but for source generators they provide a very nice entry point for debugging. Can write the test code you want to drive your source generator, run the test, and debug as your source generator processes it and creates output. Nice and clean. I only try my source generator against my actual end-usage source code when I'm mostly done with the source generator and want to give it that last final test.

  • @yourockst0ne
    @yourockst0ne 2 ปีที่แล้ว +1

    great video 😊

  • @Xania-js
    @Xania-js ปีที่แล้ว +1

    dotnet clean does not help on updating analyzer at all in my case. For everyone having th same issue, here is what I use: "dotnet build-server shutdown ; dotnet build"

  • @joga_bonito_aro
    @joga_bonito_aro 7 หลายเดือนก่อน

    Love that thumbnail

  • @thinker2273
    @thinker2273 ปีที่แล้ว +2

    It is worth noting that ISourceGenerator is deprecated and should NOT be used AT ALL. All generators should be using IIncrementalGenerator, the newer, much more performant and optimized, source generation API. This isn't just me saying this, this has been stated on multiple occasions by the compiler architects who work on Roslyn.

  • @udayughareja
    @udayughareja 2 ปีที่แล้ว +2

    i'm using source generator to create full crud module (using mutation) from db to controller and its works well slowing down the build

    • @meJevin
      @meJevin 2 ปีที่แล้ว +1

      Holy shit why would you do that

    • @udayughareja
      @udayughareja 2 ปีที่แล้ว

      @@meJevin rapid app development/ boilerplate code

  • @nocgod
    @nocgod ปีที่แล้ว +2

    could you please make a video about incremental source generators?
    I'm playing around with source generators, trying to create a service client based on an interface+models. this video really set something straight in my head
    THANKS!!!

    • @RawCoding
      @RawCoding  ปีที่แล้ว +1

      I’ll see when I get around to them

  • @ivandrofly
    @ivandrofly 9 หลายเดือนก่อน

    07:34 - Debugging
    23:30 - flag to store the file in output dir

  • @clonkex
    @clonkex ปีที่แล้ว

    28:52 _"...I think they kinda deserve it"_ *shocked pikachu face*

  • @markhill1066
    @markhill1066 4 หลายเดือนก่อน

    Crazy hot take that experienced devs debug less

  • @stringyGG
    @stringyGG ปีที่แล้ว +1

    Came here for that epic thumbnail lol.

  • @woistdasniveau8290
    @woistdasniveau8290 ปีที่แล้ว

    How do you open this window where you search for everything?

  • @asdasddas100
    @asdasddas100 ปีที่แล้ว +3

    Why not just use AppendLine with the StringBuilder? I don't understand why you're just using Append like it's the only way to use StringBuilder
    But honestly I wouldn't even use StringBuilder, I'd probably opt for the new triple equals feature.

    • @RawCoding
      @RawCoding  ปีที่แล้ว +1

      It doesn’t matter, using string builder will produce complex code in a source generator.

  • @giladfreidkin4369
    @giladfreidkin4369 2 ปีที่แล้ว +1

    How did you manage to make the source generator work for .NET6? According to docs and my experience, the generator only works when written on .netstandard 2.0 project.

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว

      Not sure what to tell you it just works

    • @ALOKSHARMAMD
      @ALOKSHARMAMD ปีที่แล้ว

      that was before the api is changed, the problem with core is it changed too many times.. good thing now that we are on .net 7 things have come to a standardized version.

  • @amnesia3490
    @amnesia3490 ปีที่แล้ว +1

    How can I share my soruce generator project as nuget package or assembly/dll reference, I couldnt really find documentation and it really doesnt work by just referencing, looks like it needs some configuration.

    • @RawCoding
      @RawCoding  ปีที่แล้ว +1

      check this out: github.com/T0shik/Shpec
      let me know if you need help

    • @amnesia3490
      @amnesia3490 ปีที่แล้ว

      @@RawCoding PackagePath="analyzers/dotnet/cs" is the key I was needed

  • @z0n_
    @z0n_ 2 ปีที่แล้ว +1

    Could you make a video about learning / debugging 3rd party code (open source)? I have Rider installed so I am able to debug some of the code with the decompilation feature but often I get the "Could Not Evaluate The Value" error when trying to inspect a variables in 3rd party code.
    Great video btw! With the new "Raw string literals" feature coming in C# 11, do you think it's still better to use the Roslyn Quoter? Seem like the code would be much more readable with "raw strings" tho more error-prone.

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว

      Thank you, on the debugging - what advice could I give except covering your specific usecase? I feel like debugging is a bit of a skill like googling, otherwise with programming your just zooming in on the issue, by finding the correct place in the execution sequence.
      as for string literals - that's a nope, you'll have alot of "logic" processing strings and values to create syntax, it's not going to be 1 neat string literal, it's going to be many little pieces put together based on your rules.

  • @udayughareja
    @udayughareja 2 ปีที่แล้ว +1

    i saw there's a new thing online, incremental source generator, what's the difference and how it is better than this?

    • @meJevin
      @meJevin 2 ปีที่แล้ว

      They are designed to take advantage of caching. Otherwise they are the same old source generators

    • @udayughareja
      @udayughareja 2 ปีที่แล้ว

      @@meJevin thanks i believe i'll use it sooner or later, my app build is very slow

  • @eddietime1811
    @eddietime1811 ปีที่แล้ว +1

    Rossynt isnt working for newer versions of Rider?

    • @RawCoding
      @RawCoding  ปีที่แล้ว

      haven't checked

  • @jaycejones2622
    @jaycejones2622 2 ปีที่แล้ว +1

    Why use the timeout and breakpoint instead of 'Debugger.Launch();'?

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว

      I've not known about this, will check it out and write back here

  • @RatzMouze
    @RatzMouze ปีที่แล้ว +1

    Anyone ever told you that you look very much like Topher Grace? specifically with his hair and beard style from the black mirror episode "smithereens"? - Just with darker hair and no glasses haha

    • @RawCoding
      @RawCoding  ปีที่แล้ว +1

      Never, I had Jesus and Golfoyle

  • @huyvole9724
    @huyvole9724 2 ปีที่แล้ว +2

    Sorry can it apply for anything? I don't understand what it will apply

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว +3

      Use code as data to create more code.

    • @huyvole9724
      @huyvole9724 2 ปีที่แล้ว

      @@RawCoding wow its great, can tou upload more video tutorial about this?

  • @IElial
    @IElial ปีที่แล้ว

    Followed this but ended catch-up by incremental police. VS yell at me I should use IIncrementalGenerator instead.

  • @aamirali8114
    @aamirali8114 5 หลายเดือนก่อน

    Why i am the only one my SourceGenerator is not generating anything i am not getting which step i missed

  • @noctavel
    @noctavel ปีที่แล้ว +1

    The way many other languages are able to generate code compared to what C# is capable of makes C# seems ridiculous

  • @joseluissuarez4093
    @joseluissuarez4093 2 ปีที่แล้ว

    ;)

  • @Plagueheart
    @Plagueheart ปีที่แล้ว +3

    You could simply add the following to Initialize() for Debugging purposes
    #if DEBUG
    if (!Debugger.IsAttached)
    Debugger.Launch();
    #endif

    • @Plagueheart
      @Plagueheart ปีที่แล้ว +1

      CTRL+SHIFT+A (Action Menu) -> Build, Execution, Deployment > Debugger -> "JIT Debugger" set Rider as a Default Debugger