Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Development with Blazor

You're reading from   Web Development with Blazor A practical guide to building interactive UIs with C# 12 and .NET 8

Arrow left icon
Product type Paperback
Published in Apr 2024
Publisher Packt
ISBN-13 9781835465912
Length 366 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jimmy Engström Jimmy Engström
Author Profile Icon Jimmy Engström
Jimmy Engström
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Hello Blazor FREE CHAPTER 2. Creating Your First Blazor App 3. Managing State – Part 1 4. Understanding Basic Blazor Components 5. Creating Advanced Blazor Components 6. Building Forms with Validation 7. Creating an API 8. Authentication and Authorization 9. Sharing Code and Resources 10. JavaScript Interop 11. Managing State – Part 2 12. Debugging the Code 13. Testing 14. Deploying to Production 15. Moving from, or Combining with, an Existing Site 16. Going Deeper into WebAssembly 17. Examining Source Generators 18. Visiting .NET MAUI 19. Where to Go from Here 20. Other Books You May Enjoy
21. Index

How to get started with source generators

It’s time to look at how we can build our source code generators. The Chapter17 folder is a finished example of what we discuss here. The instructions will not be a step-by-step guide.

To create a source code generator, we need a class library targeting .NET Standard 2.0. We also need to add a reference to the NuGet packages Microsoft.CodeAnalysis.CSharp and Microsoft.CodeAnalysis.Analyzers in that library. We also need to make sure that our .csproj file has <LangVersion>latest</LangVersion>.

To create a source generator, we need to create a class that has two things:

  • It needs to have the [Generator] attribute.
  • It needs to implement ISourceGenerator.

The template code should look something like this:

using Microsoft.CodeAnalysis;
namespace SourceGenerator;
[Generator]
public class HelloSourceGenerator : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
  ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime