Viewing Generated Code
Source generators produce code at compile time. This guide explains how to view and debug generated files in different development environments.
Before You Start
- .NET 8.0+ (or .NET 9/10 for latest features)
- A project using Excalibur.Dispatch source generators:
dotnet add package Excalibur.Dispatch.SourceGenerators.Analyzers - Familiarity with source generators
MSBuild Configuration
Add these properties to your .csproj to persist generated files to disk:
<PropertyGroup>
<!-- Emit generated files to disk -->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<!-- Location for generated files (defaults to obj/Generated) -->
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
After building, generated files appear in:
obj/
└── Debug/
└── net8.0/
└── GeneratedFiles/
└── Excalibur.Dispatch.SourceGenerators/
├── PrecompiledHandlerRegistry.g.cs
├── SourceGeneratedHandlerActivator.g.cs
├── SourceGeneratedHandlerInvoker.g.cs
└── ...
Visual Studio
Method 1: Solution Explorer (Recommended)
- Expand your project in Solution Explorer
- Expand Dependencies → Analyzers → Excalibur.Dispatch.SourceGenerators
- Generated files appear as children of the generator
The generated files appear under the Analyzers node in Solution Explorer.
Method 2: Generated Files on Disk
- Add MSBuild configuration above
- Build the project
- Navigate to
obj/Debug/net8.0/GeneratedFiles/ - Open files directly
Method 3: Go to Definition
- Right-click on a generated type (e.g.,
PrecompiledHandlerRegistry) - Select Go to Definition (F12)
- Visual Studio opens the generated source
Debugging Generated Code
- Set
EmitCompilerGeneratedFilestotrue - Open the generated
.g.csfile - Set breakpoints normally
- Run with debugging (F5)
Generated code supports full debugging including step-through, watch expressions, and conditional breakpoints.
JetBrains Rider
Method 1: Generated Files Node
- In Solution Explorer, expand your project
- Find the Generated folder under the project
- Expand Excalibur.Dispatch.SourceGenerators
- Browse generated files
Method 2: Navigate to Sources
- Place cursor on a generated type
- Press Ctrl+Click or Ctrl+B
- Rider navigates to the generated source
Method 3: Find Usages
- Right-click on a generated type
- Select Find Usages (Alt+F7)
- See all references to generated code
Rider Settings
For better source generator experience:
- File → Settings → Build, Execution, Deployment → Toolset and Build
- Enable Use RoslynAnalyzers
- Enable Include source generators in code analysis
VS Code with C# Dev Kit
Method 1: Explorer View
- Install C# Dev Kit extension
- Open project folder
- After build, navigate to
obj/GeneratedFiles/in Explorer
Method 2: OmniSharp Configuration
Create or edit .omnisharp.json in your solution root:
{
"RoslynExtensionsOptions": {
"EnableAnalyzersSupport": true,
"EnableImportCompletion": true
},
"FormattingOptions": {
"EnableEditorConfigSupport": true
}
}
Method 3: Go to Definition
- Hover over a generated type
- Ctrl+Click to navigate
- VS Code shows generated source in read-only mode
Recommended Extensions
- C# Dev Kit - Full C# language support
- C# - OmniSharp-based language support
- Solution Explorer - Visual Studio-like project tree
File Naming Conventions
Dispatch generators use consistent naming:
| Generator | Output File | Description |
|---|---|---|
| HandlerRegistrySourceGenerator | PrecompiledHandlerRegistry.g.cs | Handler registrations |
| HandlerActivationGenerator | SourceGeneratedHandlerActivator.g.cs | Handler activation |
| HandlerInvocationGenerator | SourceGeneratedHandlerInvoker.g.cs | Handler invocation |
| MessageTypeSourceGenerator | PrecompiledHandlerMetadata.g.cs | Handler metadata |
| StaticPipelineGenerator | StaticPipelines.g.cs | Static pipelines |
| DispatchInterceptorGenerator | DispatchInterceptors.g.cs | C# 12 interceptors |
| MiddlewareDecompositionAnalyzer | MiddlewareDecomposition.g.cs | Middleware analysis |
| CachePolicySourceGenerator | CacheInfoRegistry.g.cs | Cache policies |
| ServiceRegistrationSourceGenerator | GeneratedServiceCollectionExtensions.g.cs | DI registrations |
When Files Regenerate
Generated files are recreated when:
- Project is built (full or incremental)
- Source files change that affect generator input
- Generator package version changes
- Clean/rebuild is performed
Dispatch uses incremental generators that only regenerate when relevant source changes. This ensures fast build times even with many generators.
Troubleshooting
Files Not Appearing
-
Check package reference:
<PackageReference Include="Excalibur.Dispatch.SourceGenerators" Version="..." OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> -
Clean and rebuild:
dotnet clean
dotnet build -
Check build output:
dotnet build -v:detailed 2>&1 | grep -i generator
Files Are Empty
- Ensure handler interfaces are from
Excalibur.Dispatch.Abstractions - Verify classes are public and non-abstract
- Check for compilation errors that block generator execution
IDE Not Showing Generated Files
- Restart IDE after adding MSBuild configuration
- Ensure project builds successfully
- Check IDE-specific settings (see sections above)
Stale Generated Code
If generated code seems outdated:
- Clean solution:
dotnet clean - Delete
obj/andbin/folders - Rebuild:
dotnet build - Restart IDE
Comparing Generated Output
To compare generated output across builds:
# Save current state
cp -r obj/Debug/net8.0/GeneratedFiles ./generated-before
# Make changes and rebuild
dotnet build
# Compare
diff -r ./generated-before obj/Debug/net8.0/GeneratedFiles
CI/CD Considerations
For build verification:
# GitHub Actions example
- name: Build and verify generators
run: |
dotnet build -p:EmitCompilerGeneratedFiles=true
ls -la obj/Debug/net8.0/GeneratedFiles/Excalibur.Dispatch.SourceGenerators/
Generated files should NOT be committed to source control - they're build artifacts.
Related Documentation
- Source Generators - Full generator documentation
- Performance Overview - Performance benefits
- Deployment - AOT deployment guide
See Also
- Source Generators Getting Started — Step-by-step guide to enabling and configuring source generators
- Source Generators — Full reference for all 11 Dispatch source generators
- Native AOT — Native AOT compilation guide that relies on source-generated code