CLI Tool
SourceGen.Ioc.Cli is a command-line tool that helps you quickly add [IocRegister] attributes to existing classes or generate [IocRegisterFor] annotations in your project.
Installation
dotnet tool install -g SourceGen.Ioc.CliCommands
Add Attribute Command
Add [IocRegister] attribute to classes matching the specified criteria.
sourcegen-ioc add [options]Options
| Option | Description | Default |
|---|---|---|
-t, --target | Target directory or file, defaults to current directory | null (current directory) |
-f, --file-pattern | File pattern to filter files | *.cs |
-s, --search-sub-directories | Whether to search sub directories | false |
-cn, --class-name-regex | Regex pattern to match class names | null |
--full-regex | Full regex pattern to match file content | null |
--attribute-name | Name of the attribute to add | IocRegister |
-m, --max-apply | How many matches should apply, 0 means unlimited | 0 |
-n, --dry-run | Dry run, does not modify files | false |
-v, --verbose | Detailed logging message | false |
--log | Logging file path | "" |
NOTE
You must specify at least one matcher: -cn/--class-name-regex or --full-regex.
Class Name Regex
When using -cn or --class-name-regex option, the actual regex used is:
(public|internal)\s+(?!static\s+)[\w\s]*class\s+(classNameRegex)(?=\s|:|$)This matches non-static public or internal classes.
IMPORTANT
The tool automatically converts .* to \w* and .+ to \w+ in your pattern to ensure only valid identifier characters are matched.
Examples
# Search all .cs files in current directory and add attributes
sourcegen-ioc add -cn ".*"
# Search specified directory including subdirectories
sourcegen-ioc add -t ./src -s -cn ".*"
# Match only classes ending with Service
sourcegen-ioc add -cn ".*Service"
# Match only classes ending with Handler, including subdirectories
sourcegen-ioc add -cn ".*Handler" -s
# Dry run to see which files would be modified
sourcegen-ioc add -cn ".*Repository" -n -v
# Limit to 10 matches maximum
sourcegen-ioc add -cn ".*Service" -m 10
# Use full regex pattern
sourcegen-ioc add --full-regex "public\s+class\s+\w+Controller"Generate IocRegisterFor Command
Collect classes matching the specified criteria and generate a file with [IocRegisterFor] assembly attributes. This is useful when you want to register classes from external assemblies or when you prefer assembly-level attributes.
sourcegen-ioc generate ioc-for [options]Options
| Option | Description | Default |
|---|---|---|
-o, --output-path | Output file path (required) | N/A |
-t, --target | Target directory or file, defaults to current directory | null (current directory) |
-f, --file-pattern | File pattern to filter files | *.cs |
-s, --search-sub-directories | Whether to search sub directories | false |
-cn, --class-name-regex | Regex pattern to match class names (required) | N/A |
-m, --max-apply | How many matches should apply, 0 means unlimited | 0 |
-ig, --is-generic-attribute | Whether to generate generic attribute syntax | false |
-n, --dry-run | Dry run, does not create file | false |
-v, --verbose | Detailed logging message | false |
--log | Logging file path | "" |
Class Name and Namespace Extraction
When using -cn or --class-name-regex, the tool extracts the class name from regex group 2 and prepends the namespace.
NOTE
The namespace is extracted from only the first namespace declaration in each file. If a file contains multiple namespaces, only the first one will be used as the prefix for all matched classes in that file.
Examples
# Generate IocRegisterFor for all Handler classes
sourcegen-ioc generate ioc-for -o ./Generated/Handlers.g.cs -t ./src -s -cn ".*Handler"
# Generate with generic attribute syntax
sourcegen-ioc generate ioc-for -o ./Generated/Services.g.cs -cn ".*Service" -ig
# Preview output without creating file (dry-run)
sourcegen-ioc generate ioc-for -o ./output.cs -cn ".*Repository" -n -v
# Limit to 5 classes
sourcegen-ioc generate ioc-for -o ./output.cs -cn ".*Service" -m 5Output Format
The command generates a file with the following format:
Standard syntax (default):
// <auto-generated />
using SourceGen.Ioc;
[assembly: IocRegisterFor(typeof(MyApp.Services.UserService))]
[assembly: IocRegisterFor(typeof(MyApp.Services.OrderService))]
[assembly: IocRegisterFor(typeof(MyApp.Services.ProductService))]Generic syntax (with -ig flag):
// <auto-generated />
using SourceGen.Ioc;
[assembly: IocRegisterFor<MyApp.Services.UserService>]
[assembly: IocRegisterFor<MyApp.Services.OrderService>]
[assembly: IocRegisterFor<MyApp.Services.ProductService>]Generate IocRegisterDefaults Command
Collect classes matching the specified criteria and generate a file with [IocRegisterDefaults] assembly attributes. This command groups implementation classes by their base type/interface and generates registration attributes with ImplementationTypes.
sourcegen-ioc generate ioc-defaults [options]Options
| Option | Description | Default |
|---|---|---|
-o, --output-path | Output file path (required) | N/A |
-t, --target | Target directory or file, defaults to current directory | null (current directory) |
-f, --file-pattern | File pattern to filter files | *.cs |
-s, --search-sub-directories | Whether to search sub directories | false |
-cn, --class-name-regex | Regex pattern to match class names (required) | N/A |
-b, --base-type-regex | Regex pattern to match interface/base class (required) | N/A |
-m, --max-apply | How many matches should apply, 0 means unlimited | 0 |
-ig, --is-generic-attribute | Whether to generate generic attribute syntax | false |
-l, --lifetime | Lifetime for the service registration. Valid values: Singleton, Scoped, Transient | Transient |
-n, --dry-run | Dry run, does not create file | false |
-v, --verbose | Detailed logging message | false |
--log | Logging file path | "" |
NOTE
-cn and -b options must be specified together.
-l is case-insensitive and is emitted as ServiceLifetime.{PascalCase} in generated output.
Regex
When using -cn and -b options, the actual regex used is:
(public|internal)\s+(?!static\s+)[\w\s]*class\s+(classNameRegex)\s*:\s*(?:[^,{\n]+,\s*)*(baseTypeRegex)(?=\s*[,{]|$)This matches non-static public or internal classes that inherit from the specified base type/interface.
- Group 2: Class name (matched by
classNameRegex) - Group 3: Base type/interface (matched by
baseTypeRegex)
IMPORTANT
The tool sanitizes wildcard patterns before building the regex:
- For
-cn(class name):.*->\w*,.+->\w+ - For
-b(base type):.*->[\w.<>]*,.+->[\w.<>]+
Class Name and Namespace Extraction
When using -cn and -b, the tool extracts the class name from regex group 2 and the base type from regex group 3, then prepends the namespace to the class name.
NOTE
The namespace is extracted from only the first namespace declaration in each file. If a file contains multiple namespaces, only the first one will be used as the prefix for all matched classes in that file.
Examples
# Generate IocRegisterDefaults for all Handler classes implementing IHandler
sourcegen-ioc generate ioc-defaults -o ./Generated/Handlers.g.cs -t ./src -s -cn ".*Handler" -b "IHandler"
# Generate with generic attribute syntax
sourcegen-ioc generate ioc-defaults -o ./Generated/Services.g.cs -cn ".*Service" -b "I.*Service" -ig
# Preview output without creating file (dry-run)
sourcegen-ioc generate ioc-defaults -o ./output.cs -cn ".*Repository" -b "IRepository" -n -v
# Match classes with generic interface
sourcegen-ioc generate ioc-defaults -o ./output.cs -cn ".*Handler" -b "IHandler<.*>" -s
# Generate with Singleton lifetime
sourcegen-ioc generate ioc-defaults -o ./Generated/Services.g.cs -cn ".*Service" -b "I.*Service" -l SingletonOutput Format
The command generates a file with the following format:
Standard syntax (default):
// <auto-generated />
using SourceGen.Ioc;
using Microsoft.Extensions.DependencyInjection;
[assembly: IocRegisterDefaults(typeof(IHandler), ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Handlers.CommandHandler), typeof(MyApp.Handlers.QueryHandler)])]
[assembly: IocRegisterDefaults(typeof(IService), ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Services.UserService)])]Generic syntax (with -ig flag):
// <auto-generated />
using SourceGen.Ioc;
using Microsoft.Extensions.DependencyInjection;
[assembly: IocRegisterDefaults<IHandler>(ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Handlers.CommandHandler), typeof(MyApp.Handlers.QueryHandler)])]
[assembly: IocRegisterDefaults<IService>(ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Services.UserService)])]CLI Schema Command
Output CLI schema in JSON format for LLM AI.
sourcegen-ioc cli-schema [options]Options
| Option | Description | Default |
|---|---|---|
-c, --command | Command name | null (all) |
-t, --target | Target file/folder to write CLI schema | null (stdout) |
-n, --dry-run | Dry run | false |
-v, --verbose | Detailed logging message | false |
--log | Logging file path | "" |
Examples
# Output schema for all commands
sourcegen-ioc cli-schema
# Output schema for specific command
sourcegen-ioc cli-schema -c "add"
# Write schema to file
sourcegen-ioc cli-schema -t ./cli-schema.jsonUse Cases
Add DI Registration to Existing Project
When you have an existing project and want to batch add [IocRegister] attributes:
# 1. Use dry-run first to preview which files will be affected
sourcegen-ioc add -t ./src/MyProject -s -cn ".*Service" -n -v
# 2. Execute after confirming the changes
sourcegen-ioc add -t ./src/MyProject -s -cn ".*Service"Generate Registration file
When you want to register classes in one file:
# Generate IocRegisterFor attributes for all handlers
sourcegen-ioc generate ioc-for -o ./Generated/Handlers.g.cs -t ../Project/src -s -cn ".*Handler"Generate Defaults Registration file
When you want to register classes grouped by their base type/interface:
# Generate IocRegisterDefaults for handlers grouped by interface
sourcegen-ioc generate ioc-defaults -o ./Generated/HandlerDefaults.g.cs -t ./src -s -cn ".*Handler" -b "IHandler"
# Generate for multiple base types
sourcegen-ioc generate ioc-defaults -o ./Generated/Defaults.g.cs -t ./src -s -cn ".*" -b "I.*"Add Attributes to Handler Classes
sourcegen-ioc add -t ./src/Handlers -s -cn ".*Handler"Add Attributes to Repository Classes
sourcegen-ioc add -t ./src/Repositories -s -cn ".*Repository"