Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a Source Generator That Silently Stops Running Because the Partial Class Was Declared in a Different Assembly

- 22.10.26 - ErcanOPAK

🔧 The Generated Code That Was Never Going to Appear

A C# source generator only ever sees the syntax trees of the compilation it’s attached to – it has no visibility into other assemblies’ source code at all, even ones referenced by the project – so a partial class intended to receive generator-produced members has to have ALL of its partial declarations inside the same assembly the generator is actually running against; splitting the class across a shared base project and a consuming project (a common pattern for sharing partial classes across multiple projects) means the generator simply never sees the declaration it’s supposed to be completing, and produces no output and no error for that type at all.

🔎 The Problem

// In SharedModels.csproj:
public partial class Order
{
    public int Id { get; set; }
}

// In ApiProject.csproj, which references SharedModels AND has the
// source generator package installed:
[GenerateValidation] // a marker attribute the generator looks for
public partial class Order
{
}

// The generator runs during ApiProject's compilation and can see
// THIS partial declaration and its [GenerateValidation] attribute -
// but it has no access to SharedModels' source at all, since that
// assembly was already compiled into a binary reference by the
// time ApiProject builds. If the generator's logic needs to see
// the Id property to generate anything meaningful against it,
// it silently produces nothing useful, with no error raised.

✅ Fix: Keep Every Partial Piece the Generator Needs in One Assembly

  • Move all partial declarations of a type that a source generator needs to read in full into the same project the generator actually runs in – if a class needs to be shared across multiple consuming projects, consider whether the generator should run in each consuming project (referencing the generator as a package, not just the output of running it once) rather than assuming it can see declarations living in a separately-compiled, already-referenced assembly.
  • If the design genuinely requires splitting the type across projects, have each project’s source generator package regenerate its own members from only what’s locally visible, and design the generator’s logic to not depend on information that only exists in a different compilation unit’s syntax tree.
  • Add a build-time check or generator diagnostic (reporting via SourceProductionContext.ReportDiagnostic) when the generator detects a marker attribute on a partial class but can’t find enough context to generate anything meaningful – turning a silent no-op into a visible build warning makes this mistake immediately obvious instead of only showing up as ‘why isn’t the generated method here’.

🚨 Why This Is Dangerous

  • The project builds completely successfully with zero errors and zero warnings – a source generator that finds nothing actionable simply produces no output for that type, which is indistinguishable at build time from a generator that correctly determined there was nothing to generate.
  • This specifically breaks when a working single-project prototype gets refactored into a multi-project solution for better separation of concerns – code that worked perfectly during initial development silently stops generating anything the moment the partial class gets split across the new project boundary, with the refactor itself looking completely unrelated to source generation.

A source generator can only complete a partial class from what it can actually see – and once a compilation boundary sits between two pieces of the same partial type, one of those pieces is permanently invisible to any generator running on the other side of it.

— C# Best Practice

Related posts:

Events Cause Memory Leaks

Events Cause Memory Leaks

Background Tasks with IHostedService

Background Tasks with IHostedService

C#: Real-world LINQ Query Examples

C#: Real-world LINQ Query Examples

Views: 8

Post navigation

ASP.NET Core: Fix a SignalR Group Message That Silently Never Reaches a Client Because the Group Name Has a Trailing Space Mismatch
Windows 11: Fix the Start Menu’s Recommended Section Silently Showing Files From a Different User Account

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com