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 10.0
- 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/
└── net10.0/
└── GeneratedFiles/
└── Excalibur.Dispatch.SourceGenerators/
├── CacheInfoExtractor.g.cs
├── CacheInfoRegistry.g.cs
├── DiscoveredMessageTypeMetadata.g.cs
├── DiscoveredMessageTypeRegistry.g.cs
├── EventMetadataContent.g.cs
├── EventStoreTypeMap.g.cs
├── FluentValidationDispatcher.g.cs
├── GeneratedHandlerActivatorRegistrations.g.cs
├── GeneratedHandlerRegistrationExtensions.g.cs
├── GeneratedSagaRegistrationExtensions.g.cs
├── GeneratedServiceCollectionExtensions.g.cs
├── HandlerInvokerRegistry.g.cs
├── MiddlewareDecomposition.g.cs
├── MiddlewareInvokers.g.cs
├── PipelineMetadata.g.cs
├── PrecompiledDirectActionDispatch.g.cs
├── PrecompiledHandlerMetadata.g.cs
├── PrecompiledHandlerRegistry.g.cs
├── PrecompiledSagaMetadata.g.cs
├── ProjectionTagResolverRegistry.g.cs
├── ResultFactoryRegistry.g.cs
├── StaticPipelines.g.cs
├── TypedDispatchExtensions.g.cs
└── *Validator.g.cs (one per validated type)
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/net10.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(s) | Description |
|---|---|---|
| HandlerRegistrySourceGenerator | PrecompiledHandlerRegistry.g.cs, PrecompiledHandlerMetadata.g.cs, GeneratedHandlerRegistrationExtensions.g.cs, GeneratedHandlerActivatorRegistrations.g.cs, PrecompiledDirectActionDispatch.g.cs | Handler registrations, metadata, activators, and direct-action dispatch |
| DispatchActionExtensionGenerator | TypedDispatchExtensions.g.cs | Typed dispatch with TResponse inference |
| HandlerInvokerSourceGenerator | HandlerInvokerRegistry.g.cs | Handler invocation registry |
| MessageTypeSourceGenerator | GeneratedMessageTypeRegistrations.g.cs | Message type registrations |
| MessageResultExtractorGenerator | ResultFactoryRegistry.g.cs | Result factory registry for message result extraction |
| StaticPipelineGenerator | StaticPipelines.g.cs | Static pipelines |
| MiddlewareDecompositionAnalyzer | MiddlewareDecomposition.g.cs | Middleware analysis |
| MiddlewareInvokerInterceptorGenerator | MiddlewareInvokers.g.cs | Middleware invoker interceptors |
| CachePolicySourceGenerator | CacheInfoRegistry.g.cs | Cache policies |
| CacheInfoSourceGenerator | CacheInfoExtractor.g.cs | Cache info extraction |
| ProjectionTagResolverGenerator | ProjectionTagResolverRegistry.g.cs | Projection cache tag resolution |
| ServiceRegistrationSourceGenerator | GeneratedServiceCollectionExtensions.g.cs | DI registrations |
| EventStoreTypeMapGenerator | EventStoreTypeMap.g.cs, EventMetadataContent.g.cs | Event store type mappings and metadata |
| JsonSerializationSourceGenerator | DiscoveredMessageTypeRegistry.g.cs, DiscoveredMessageTypeMetadata.g.cs | JSON serialization message type discovery |
| FluentValidationGenerator | FluentValidationDispatcher.g.cs | Fluent validation integration |
| AotValidationGenerator | *Validator.g.cs | AOT-compatible validators (one per validated type) |
| SagaRegistrationGenerator | GeneratedSagaRegistrationExtensions.g.cs | Saga DI registrations |
| SagaMetadataGenerator | PrecompiledSagaMetadata.g.cs | Saga metadata |
| PipelineDeterminismAnalyzer | PipelineMetadata.g.cs | Pipeline determinism analysis metadata |
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 cleandotnet build -
Check build output:
dotnet build -v:detailed 2>&1 | grep -i generator
Files Are Empty
- Ensure handler interfaces are from the
Excalibur.Dispatch.Deliverynamespace - 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/net10.0/GeneratedFiles ./generated-before
# Make changes and rebuild
dotnet build
# Compare
diff -r ./generated-before obj/Debug/net10.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/net10.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