惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园_首页
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
美团技术团队
GbyAI
GbyAI
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Security @ Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Jina AI
Jina AI
The Cloudflare Blog
O
OpenAI News
雷峰网
雷峰网
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
量子位
C
CERT Recently Published Vulnerability Notes
V
V2EX
J
Java Code Geeks
H
Help Net Security
小众软件
小众软件
H
Hacker News: Front Page
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Scott Helme
Scott Helme
N
News | PayPal Newsroom
Vercel News
Vercel News
爱范儿
爱范儿
TaoSecurity Blog
TaoSecurity Blog
Webroot Blog
Webroot Blog
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
L
Lohrmann on Cybersecurity
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
S
Schneier on Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Source Generators in .NET: Build a Zero-Reflection Command Dispatcher
Shathish · 2026-06-25 · via DEV Community

In Part 1, we built a generator that found classes marked [AutoToString] and emitted a ToString() override for them. It was a good first generator precisely because it didn't have much else going on — find a class, check an attribute, write a file. This time we're pointing the same incremental pipeline at something with more architectural payoff: a command dispatcher.

Most dispatchers — MediatR, a hand-rolled one, whatever you've used — resolve handlers at runtime. They scan assemblies, walk type hierarchies, hit a dictionary or a service locator to figure out which handler runs which command. That works, but it costs reflection at startup, allocations you can't see, and errors that only show up when the app is already running.

What if the dispatcher was just a switch statement? One the compiler wrote for you, based on whatever handlers actually exist in your project?

That's what we're building. By the end, a class decorated with [RegisterHandler] will get picked up automatically, and dispatching a command will look like an ordinary method call — no reflection, no container, just a generated switch.

What We're Building Toward

// <auto-generated />
namespace MySourceGenerator;

public partial class InlineDispatcher
{
    public void Dispatch(object command)
    {
        if (command is null) return;

        switch (command)
        {
            case MyApp.Commands.CreateUserCommand cmd:
                new MyApp.Handlers.CreateUserHandler().Handle(cmd);
                break;

            default:
                throw new InvalidOperationException(
                    $"No handler registered for command type: {command.GetType().FullName}.");
        }
    }
}

Every case arm in that file was written by a generator. You never touch it — add a new handler, rebuild, and a new case shows up.

The project setup is identical to Part 1: generator project on netstandard2.0, referenced with OutputItemType="Analyzer" and ReferenceOutputAssembly="false", same syntax-then-semantics pipeline shape. I won't re-walk that part — if it's unfamiliar, that's what Part 1 covers. What's new here is everything specific to dispatching: carrying structured data through the pipeline instead of a bare syntax node, and aggregating many classes into one generated file instead of one file per class.

A Small Wrinkle Before We Start: Records on netstandard2.0

The generator is going to lean on a record to carry data between pipeline stages (more on why in a moment). Records need init-only setters, which rely on System.Runtime.CompilerServices.IsExternalInit under the hood — a type that exists in .NET 5+ but not in netstandard2.0. And the generator project is stuck on netstandard2.0 no matter what <LangVersion> you set.

The fix is one of those one-line shims that's annoying to discover and trivial once you know it:

// IsExternalInit.cs — inside the generator project
namespace System.Runtime.CompilerServices
{
    internal static class IsExternalInit { }
}

Declaring this empty class satisfies the compiler without needing the real runtime type. You'll see this exact polyfill in most generator projects that use records or init — it's a known trick, not a hack you invented.

The Data Model: HandlerInfo

Part 1's generator never needed to carry data between pipeline stages — it found a class, checked an attribute, and emitted a file right there. A dispatcher is different: we need to gather information about every handler in the project before we can write a single switch statement. That means defining a small data model to carry through the pipeline:

namespace LightweightDispatcher.Generator;

public record HandlerInfo(string CommandType, string HandlerType);

Just two fully-qualified type names. But the choice of record over class, and string over a Roslyn symbol, isn't incidental — both decisions are load-bearing.

It's a record, not a class, because the incremental pipeline caches results between runs by comparing outputs for equality. Records give you structural equality for free — two HandlerInfo instances with the same strings are equal, full stop. A plain class compares by reference, so Roslyn would conclude every run produced "new" data and re-run everything downstream — which defeats the entire point of using IIncrementalGenerator in the first place.

It stores strings, not INamedTypeSymbol, because Roslyn symbols are tied to a specific compilation snapshot. Hold onto one across pipeline stages and you've got a memory leak and broken caching waiting to happen. The rule here is absolute: never store a Roslyn symbol in a pipeline data model. Pull out what you need as a string immediately, and carry that instead.

Building the Generator

[Generator]
public sealed class DispatcherGenerator : IIncrementalGenerator
{
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        // ...
    }
}

sealed because there's no reason to subclass a generator. Initialize runs exactly once, at load time — its job is to describe the pipeline, not run it. The actual scanning and emitting happens lazily, driven by Roslyn, every time something relevant changes.

Injecting the Attribute

Same trick as Part 1: before any user code is analyzed, inject the marker attribute so it exists in the compilation without the consumer needing a separate package.

context.RegisterPostInitializationOutput(static postInitContext =>
{
    const string attribSource = @"
        namespace MySourceGenerator;

        [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
        public sealed class RegisterHandlerAttribute : System.Attribute
        {
        }
    ";

    postInitContext.AddSource("RegisterHandlerAttribute.g.cs", attribSource);
});

AllowMultiple = false means a class can only carry one [RegisterHandler]. Inherited = false means it doesn't flow down to subclasses — each handler has to opt in explicitly.

Notice the static on that lambda. It's not a style preference — it's enforced statelessness. Generators have to be stateless between runs; an accidental closure over this or a captured local is exactly the kind of thing that silently breaks Roslyn's caching without throwing an error anywhere.

Finding Handlers — and What Their Handle Method Accepts

The syntax-then-semantics shape is the same two-phase filter from Part 1: a cheap predicate runs on every node in the compilation, and only survivors get the expensive semantic treatment.

var classesWithAttributes = context.SyntaxProvider.CreateSyntaxProvider(
        predicate: static (node, _) => IsClassWithAttributes(node),
        transform: static (ctx, _) => GetSemanticTargetForGeneration(ctx))
    .Where(static target => target is not null);

private static bool IsClassWithAttributes(SyntaxNode node)
    => node is ClassDeclarationSyntax { AttributeLists.Count: > 0 };

Where it gets more interesting is the semantic transform. Part 1's version only had to answer "does this attribute resolve to the right type?" This one has to go further — once it confirms the attribute, it has to dig into the class and find out what command type its Handle method actually accepts:

private static HandlerInfo? GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
{
    var classDecl = (ClassDeclarationSyntax)context.Node;

    foreach (var attributeList in classDecl.AttributeLists)
    {
        foreach (var attribute in attributeList.Attributes)
        {
            if (context.SemanticModel.GetSymbolInfo(attribute).Symbol
                    is not IMethodSymbol attributeConstructor)
                continue;

            var attributeType = attributeConstructor.ContainingType;

            if (attributeType.ToDisplayString() == "MySourceGenerator.RegisterHandlerAttribute")
            {
                var handlerSymbol = context.SemanticModel
                    .GetDeclaredSymbol(classDecl) as INamedTypeSymbol;

                if (handlerSymbol is null) return null;

                var handleMethod = handlerSymbol.GetMembers("Handle")
                    .OfType<IMethodSymbol>()
                    .FirstOrDefault();

                if (handleMethod is not null && handleMethod.Parameters.Length == 1)
                {
                    var commandFullName = handleMethod.Parameters[0].Type.ToDisplayString();
                    var handlerFullName = handlerSymbol.ToDisplayString();
                    return new HandlerInfo(commandFullName, handlerFullName);
                }
            }
        }
    }

    return null;
}

A detail that's easy to walk past: GetSymbolInfo(attribute).Symbol hands back an IMethodSymbol, not the attribute type itself. Applying an attribute is, under the hood, a constructor call — so the symbol you get back is that constructor, and you reach the attribute's type via .ContainingType. It's a small thing, but it's the reason the pattern match is written the way it is.

Once the attribute checks out, the method goes looking for a Handle method on the class and reads the type of its single parameter. That parameter type — fully qualified, via ToDisplayString() — is the command type. Pair it with the handler's own fully qualified name, and that's everything the dispatcher needs to know about this one handler.

Gathering Every Handler Into One File

This is where the shape genuinely diverges from Part 1. The AutoToString generator emitted one file per matching class, completely independently of every other class. A dispatcher can't work that way — every handler's case has to land in the same file, because there's only one switch statement.

IncrementalValueProvider<ImmutableArray<HandlerInfo?>> gatheredHandlers =
    classesWithAttributes.Collect();

context.RegisterSourceOutput(gatheredHandlers, static (productionContext, handlers) =>
{
    var generatedCode = GenerateDispatcherSource(handlers);
    productionContext.AddSource("InlineDispatcher.g.cs", generatedCode);
});

Before .Collect(), classesWithAttributes is a stream — one HandlerInfo? per matching class, each independent. .Collect() folds that stream into a single ImmutableArray holding every result at once. That's the signal for when to reach for it: use .Collect() when the output genuinely depends on all the matches together, and skip it when each match can produce its own output in isolation, the way Part 1's classes did.

Writing the Switch Table

private static string GenerateDispatcherSource(ImmutableArray<HandlerInfo?> handlers)
{
    var switchCases = new System.Text.StringBuilder();

    foreach (var handler in handlers)
    {
        if (handler is null) continue;

        switchCases.AppendLine($@"            case {handler.CommandType} cmd:");
        switchCases.AppendLine($@"                new {handler.HandlerType}().Handle(cmd);");
        switchCases.AppendLine($@"                break;");
    }

    return $$"""
             // <auto-generated />
             namespace MySourceGenerator;

             public partial class InlineDispatcher
             {
                 public void Dispatch(object command)
                 {
                     if (command is null) return;

                     switch (command)
                     {
                        {{switchCases}}
                         default:
                             throw new System.InvalidOperationException(
                                 $"No handler registered for command type: {command.GetType().FullName}.");
                     }
                 }
             }
             """;
}

Part 1 ran into the brace-escaping problem head-on — {{{{ to push one literal { through two layers of string interpolation. This generator sidesteps it entirely with a raw string literal using a custom delimiter: $$"""...""". With the double-dollar prefix, only {{...}} triggers interpolation; single { and } are just characters. The whole switch block's native braces can sit there untouched. Same underlying problem as Part 1, cleaner tool for it.

Two things worth a second look in the generated code itself:

  • case CommandType cmd: is a type-pattern switch. Inside that case body, cmd is already typed as the specific command — Handle(cmd) needs no cast.
  • new {handler.HandlerType}().Handle(cmd) constructs the handler directly with new. No container, no factory — which is exactly the "lightweight" part of the name, and exactly the thing we'll outgrow once handlers need constructor dependencies.

partial is doing a different job here than it did in Part 1. There, it let the generator bolt a method onto a class you'd written. Here, the generator owns InlineDispatcher outright — partial just leaves the door open for you to add fields or constructors to it in your own file, without ever touching the generated one.

Seeing It Work

Here's a small console app wired up against the generator, with two handlers:

// Commands.cs
namespace MyApp.Commands;

public record CreateUserCommand(string Username);
public record DeactivateUserCommand(int UserId);

// Handlers.cs
using MySourceGenerator;
using MyApp.Commands;

namespace MyApp.Handlers;

[RegisterHandler]
public class CreateUserHandler
{
    public void Handle(CreateUserCommand command)
        => Console.WriteLine($"Creating user '{command.Username}'");
}

[RegisterHandler]
public class DeactivateUserHandler
{
    public void Handle(DeactivateUserCommand command)
        => Console.WriteLine($"Deactivating user #{command.UserId}");
}

// No [RegisterHandler] — the dispatcher won't know about this one.
public class AuditLogger
{
    public void Handle(CreateUserCommand command)
        => Console.WriteLine("This never runs — not registered.");
}

// Program.cs
using MyApp.Commands;
using MySourceGenerator;

var dispatcher = new InlineDispatcher();

dispatcher.Dispatch(new CreateUserCommand("adolphous"));
dispatcher.Dispatch(new DeactivateUserCommand(42));

try
{
    dispatcher.Dispatch("not a command");
}
catch (InvalidOperationException ex)
{
    Console.WriteLine($"Caught: {ex.Message}");
}

Output:

Creating user 'adolphous'
Deactivating user #42
Caught: No handler registered for command type: System.String.

InlineDispatcher never existed as a file you wrote. Add a third [RegisterHandler] class tomorrow, rebuild, and a third case shows up in it with no further wiring. Drop the attribute from a handler, like AuditLogger above, and the generator simply never sees it — no error, no case, no dispatch.

If you add <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> to the app's .csproj like we did in Part 1, the generated file under obj/Debug/net10.0/generated/.../InlineDispatcher.g.cs looks exactly like the target we sketched out at the start of this post — just with both case arms filled in.

The Full Incremental Picture

Roslyn parses .cs files → SyntaxTrees

PostInitializationOutput (once, cached)
→ injects RegisterHandlerAttribute.g.cs

SyntaxProvider walks every SyntaxNode
→ Predicate: IsClassWithAttributes          [syntax only, runs on everything]
→ Transform: GetSemanticTargetForGeneration [semantic, runs on survivors]
→ .Where(not null)                          [drops non-matching classes]
→ .Collect()                                [gathers into ImmutableArray]

RegisterSourceOutput
→ GenerateDispatcherSource builds the switch table
→ AddSource injects InlineDispatcher.g.cs

Roslyn compiles: user code + attribute file + dispatcher file

Edit some unrelated file and rebuild, and Roslyn re-runs the predicate, gets the same passing nodes, the transform produces HandlerInfo records that are structurally equal to last time, .Collect() produces an identical array — and RegisterSourceOutput never fires. The dispatcher file from the previous build is reused untouched. That's the entire reason the record choice mattered earlier: without value equality, every build would look "changed," and the caching shown in this diagram wouldn't happen at all.

Gotchas, Ranked by How Quietly They Fail

  1. Using a plain class instead of a record for pipeline data. Compiles fine, generator runs fine — it just re-emits on every build instead of caching, and you won't notice unless you're watching build times or IDE responsiveness.
  2. Forgetting the IsExternalInit polyfill when a pipeline model uses record or init. This one isn't quiet — it's a hard compile error on the generator project — but it's a confusing one if you don't already know netstandard2.0 is missing the type.
  3. Holding an INamedTypeSymbol (or any ISymbol) in a data model that survives past a single pipeline stage. No immediate error. Just memory growth and caching that mysteriously stops working as the project grows.
  4. Reaching for .Collect() on output that didn't need aggregating. Not wrong, exactly — but it forces every downstream emission to re-run whenever any single handler changes, instead of only the one that actually changed.
  5. A handler class without a Handle method, or with the wrong parameter count. GetSemanticTargetForGeneration just returns null for it — same as a class that never had [RegisterHandler] in the first place. No warning, no case in the switch, nothing dispatched. Worth an analyzer in its own right, but that's outside the scope of this post.

What's Next

This dispatcher works, but it instantiates handlers with new, which rules out constructor dependencies entirely. In the next post — the last in this trio — we'll build a generator that scans for IHandler<TCommand> implementations instead of an attribute, resolves handlers via IServiceProvider, and emits an AddLightweightDispatcher() DI registration extension alongside the dispatcher itself.