C# Source Generators – Why and How
Автор: Microsoft Visual Studio
Загружено: 2025-12-16
Просмотров: 5679
In this recorded Live! 360 session, you’ll learn how C# source generators work, why they matter, and how they can dramatically improve app performance by eliminating runtime reflection, reducing allocations, and enabling ahead-of-time (AOT) execution. Jim Wooley walks through real examples, from logging and regex optimization to JSON serialization, and shows how incremental generators improve build performance and keep generated code in sync as you type.
You’ll also explore how to write your own source generator using Roslyn APIs, understand syntax trees and semantic models, and validate generators with unit tests. If you're building .NET apps and want to automate boilerplate, remove reflection, or tune your app for speed, this session gives you the foundations to get started.
🔑 What You’ll Learn
• What source generators are and how they fit into meta-programming
• How generators eliminate reflection and support AOT compilation
• Real examples: logging, regex, JSON, validation, mappers
• How incremental generators improve performance over older models
• How to explore Roslyn syntax trees & semantic models
• How to write your own generator using attributes & visitors
• Unit testing and debugging approaches for source generators
• Performance impacts and comparisons (AutoMapper, Mapster, mocking libs)
⏱️ Chapters
00:00 Intro + What source generators are & why they exist
02:54 Eliminating reflection and enabling AOT
05:44 How source generators differ from GenAI
08:05 Examples in .NET: logging, regex, JSON, validation
11:58 Demo: Logging generator & partial methods
14:46 Demo: Regex generator & performance benefits
16:14 Demo: JSON serialization generator
17:40 Live change-tracking: how generators respond to edits
19:20 Using attributes to trigger generation
20:00 Intro to Roslyn syntax trees & code analysis
26:05 Navigating syntax with the Syntax Visualizer
29:32 Understanding attributes & metadata via Roslyn APIs
31:00 Demo: Building a simple CSV generator
35:55 Classic (non-incremental) generator architecture
38:30 Walking the syntax tree & extracting semantic info
41:52 Generating new code with StringBuilder
43:10 Incremental generators & why they matter
45:30 Transformations, caching, and performance optimization
47:25 Using records and value semantics in generator pipelines
49:00 Executing generation & adding output to the compilation
50:55 Demo: Extended CSV generator with new properties
54:00 How to test generators with xUnit & Verify snapshots
58:30 Debugging generators (best practices & pitfalls)
1:01:00 How Roslyn builds trees, tokens, trivia & factories
1:01:49 When meta-programming makes sense (mapping, DTOs, reflection removal)
1:03:00 Performance comparisons (AutoMapper, Mapster, mocking frameworks)
1:06:20 Licensing, open source considerations & practical guidance
1:08:48 Q&A + extending the sample generator
1:10:03 Additional resources & where to go next
👤 Speaker: Jim Wooley
Senior Principal, Slalom Consulting
🔗 Links
• Jim's Roslyn Labs repo: https://github.com/jwooley/RoslynLabs
• Explore more Live! 360 sessions: https://aka.ms/L360Orlando25
• Check out upcoming VS Live! events: https://aka.ms/VSLiveEvents
#csharp #dotnet #visualstudio #roslyn
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: