Coding Shorts: Building a Source Generator - Part 1

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ค. 2024
  • Part 2 is now live: • Coding Shorts: Buildin...
    My new live, online courses are coming in April and May:
    ASP.NET Core: Building Sites and APIs
    April 11-13th, 2023
    shawnl.ink/aspnetcore-live
    Building Apps with Vue, Vite and TypeScript
    May 9-11th, 2023
    shawnl.ink/vue-live
    --------------------------------------------------------
    I needed to build a Source Generator for my MinimalApiDiscovery project. Let's dig into how they're built! Let's look at it:
    00:00 - Introduction
    01:12 - Getting Started
    02:32 - Creating the Generator Class
    04:36 - The Syntax Provider
    08:30 - The Compilation Provider
    09:22 - Registering an Output Source
    10:22 - Creating the Source Code
    12:47 - Creating a Test Project
    13:38 - Referencing the Generator
    14:46 - Seeing the Generated Code
    16:13 - Wrapping Up
    Source Code: github.com/shawnwildermuth/co...
    If you like this video, you might like other videos in my Instructional Videos:
    - • Instructional Videos
    You can hire me too! You can reach me at my new website:
    - shawn.wildermuth.com
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @draganradovac8803
    @draganradovac8803 หลายเดือนก่อน +1

    That was really cool. Thanks

  • @kodikodi9948
    @kodikodi9948 4 หลายเดือนก่อน +1

    this is exactly what i needed. thank you.

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

    I always learn so many extra useful things from your videos I love it I always get way more than i planned for and you do it without wasting any time .

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

    Really like your videos. Always find something interesting in them.

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

    Your latest content is awesome!

  • @ronnieoverby
    @ronnieoverby 6 หลายเดือนก่อน

    I love this guy. Thanks, Shawn!

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

    Interesting topic, thanks!

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

    Great video! By the way you can drag the class library project and drop it into the console project, this feature saved me opening the add reference window

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

    Hi Mr Shawn, It was a wonderful video about code generation!! :) ;) I thought that one of the ways to generate code was the T4 templates, but along with this, I think is another thing to do w/ code generate!! Thx You!

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

    Thanks for the video, does part 2 comes next week?

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

    Hi Shawn, will your SG series cover debugging/unit testing source generators?

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

      No unit testing, though it just builds code, so unit testing the generated code shouldn't be too difficult. The second episode (this week) will cover how to read the Roslyn data to build up your own code and how to debug it in Visual Studio. Look for it Thursday.

  • @the-nasim
    @the-nasim ปีที่แล้ว

    Please make a video about refresh tokens.

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

    Thanks for the great video. I probobly would not have watched this but it was suggested by youtube's magic suggestion engine... so I did. I think I might re-watch it and experiment with this... anything as an alternative to reflection! =)

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

      Thank you!

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

      @@swildermuth - fyi - I built it but it ended up not working for some reason. The 'using SampleSourceGenerator' line had an error here with a type or namespace name 'type/namespace' could not be found. I added the reference and the Analyzer line in for the project but no luck. I added the project to github but don't want to add it to add it because the comment might be removed.. this is my 3rd post of this text.

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

    Could the Where clause be removed and instead the nullcheck be added to the predicate?

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

      I don't think so, as the predicate is fired as the code changes, the null check to to ensure that the predicates are not null, not the result.

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

    The idea of SG is cool, but it seems that the tools (VS/rider) is not that completely ready.

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

      Shouldn't matter, on tools that do not use them, they are executed at compile time too. Lots of tools you're already using probably employ them and you never noticed.

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

      @@swildermuth I think the SG can be a tool that is biased towards the needs of individual developers, so it is better to be easy to use. But its current usage and syntax do not seem so approachable, and if the tool developed for it is not user-friendly neither, ordinary developers may feel that this tool is inaccessible.

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

      That's why I decided to do the video, to make it more approachable.

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

    Topic is interesting. However, the lack of explicit type declaration made it hard to understand.
    I also wish you didn't follow the code, but rather lead the video. Nick Chapsas does an excellent job that way. His narration is very good.

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

      Not sure what you mean , rather not follow the code.

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

    yeah but what even IS a source generator?

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

      Not to be pedantic, but it's code that generates source based on the code you're writing. Generators run while roslyn is parsing your code while you're writing in the editor as well as at build-time.