Intent.Modules.Common.CSharp module provides comprehensive support for generating C# code, including template base classes, fluent code builders, and type resolution utilities.
Template Base Class
All C# templates inherit fromCSharpTemplateBase<TModel>, which provides:
- Automatic using directive management: Automatically adds and organizes
usingstatements - Type resolution: Resolves fully qualified type names while avoiding conflicts
- NuGet dependency management: Register package dependencies for your generated code
- Namespace normalization: Intelligently shortens type references based on context
- Roslyn-based code merging: Safely merge generated code with user modifications
Basic Template Structure
Key Properties and Methods
C# Code Builder (CSharpFile)
TheCSharpFile builder provides a fluent API for constructing C# code programmatically:
Building Classes
Advanced Features
- Attributes
- Nested Types
- Code Blocks
- XML Comments
Type Resolution
Intent’s C# type resolver intelligently handles:- Namespace conflicts: Automatically qualifies types when there are naming conflicts
- Generic types: Properly formats generic types with their type parameters
- Nullable reference types: Respects C# 8+ nullable annotations
- Collection types: Formats collection types according to configured patterns
File Configuration
TheCSharpFileConfig class controls how files are generated:
Best Practices
Use the Builder API for Complex Code
Use the Builder API for Complex Code
For anything beyond simple string templates, use
CSharpFile and its builder API. It handles indentation, formatting, and syntax correctly.Leverage Type Resolution
Leverage Type Resolution
Always use
GetTypeName() and related methods instead of hardcoding type names. This ensures proper namespace management and conflict resolution.Register Dependencies Early
Register Dependencies Early
Call
AddNugetDependency() and AddTypeSource() in your template constructor to ensure dependencies are available during code generation.Use OnBuild for Complex Configuration
Use OnBuild for Complex Configuration
For complex builder scenarios, use
CSharpFile.OnBuild() to defer configuration until after all decorators have run:See Also
Java Support
Generate Java code with similar capabilities
TypeScript Support
Generate TypeScript and JavaScript code
Template Basics
Learn about Intent templates
Code Management
Understand how Intent merges generated code