A deep dive into building performant incremental source generators in C# using Roslyn. Covers using ForAttributeWithMetadataName (99% fewer nodes than CreateSyntaxProvider), enforcing static lambdas to prevent captures, the filter-early/transform-late layering pattern, how to safely combine providers without destroying cache incrementality, handling AdditionalTexts, and fixing the ImmutableArray equality trap after Collect(). Includes a complete worked AutoToString generator example and five concise rules for keeping generators incremental.
Table of contents
ForAttributeWithMetadataName: The Modern Entry PointFilter Early, Transform LateCombining Providers Without Killing the CacheAdditionalTexts Done RightCollect and the Batching TrapA Complete Worked GeneratorThe Rules, In One Place3.5K Impressions