MSBuild Configuration
SourceGen.Ioc reads several MSBuild properties during generation.
Start with this minimal setup in your .csproj:
xml
<PropertyGroup>
<SourceGenIocName>MyApp</SourceGenIocName>
</PropertyGroup>Properties Reference
| Property | Default | Description |
|---|---|---|
SourceGenIocName | Assembly name | Name used by generated Add{Name} extension methods. |
SourceGenIocDefaultLifetime | Transient | Fallback lifetime when no explicit/default lifetime is provided. |
SourceGenIocFeatures | Register,Container,PropertyInject,MethodInject | Feature flags controlling generated outputs and injection member support. |
RootNamespace | Assembly name | Namespace used for generated code output. |
SourceGenIocName
Use this to set the generated registration method name:
xml
<PropertyGroup>
<SourceGenIocName>MyApp</SourceGenIocName>
</PropertyGroup>Generated Code
csharp
// <auto-generated/>
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMyApp(this IServiceCollection services)
{
// registration code...
return services;
}
}SourceGenIocDefaultLifetime
Use this to change global lifetime fallback:
xml
<PropertyGroup>
<SourceGenIocDefaultLifetime>Scoped</SourceGenIocDefaultLifetime>
</PropertyGroup>Lifetime decision order is:
- Explicit lifetime on
[IocRegister]/[IocRegisterFor] - Matching
[IocRegisterDefaults] SourceGenIocDefaultLifetime- Built-in fallback
Transient
Generated Code
csharp
// <auto-generated/>
// No explicit lifetime on attribute, and no matching defaults
// -> generator uses SourceGenIocDefaultLifetime=Scoped
services.AddScoped<global::MyNamespace.MyService, global::MyNamespace.MyService>();SourceGenIocFeatures
Enable/disable generated outputs and injection member kinds:
xml
<PropertyGroup>
<SourceGenIocFeatures>Register,Container,PropertyInject</SourceGenIocFeatures>
</PropertyGroup>Available Feature Flags
| Flag | Description |
|---|---|
Register | Generate IServiceCollection extension output. |
Container | Generate container output for [IocContainer] classes. |
PropertyInject | Enable [IocInject] support on properties. |
FieldInject | Enable [IocInject] support on fields. |
MethodInject | Enable [IocInject] support on methods. |
NOTE
FieldInject is available but not included in the default feature set. Add it explicitly when you want field injection generation.
Parsing behavior:
- Comma-separated values.
- Case-insensitive matching.
- Whitespace around values is ignored.
- Invalid values are ignored.
Generated Code
csharp
// <auto-generated/>
// SourceGenIocFeatures: Register,Container,PropertyInject
// MethodInject and FieldInject disabled -> only property injection is emitted.
services.AddSingleton<global::MyNamespace.MyService>((global::System.IServiceProvider sp) =>
{
var s0_p0 = sp.GetRequiredService<global::MyNamespace.IMyDependency>();
var s0 = new global::MyNamespace.MyService() { Dependency = s0_p0 };
return s0;
});WARNING
If a member uses [IocInject] but its feature flag is disabled, analyzer SGIOC022 is reported.
RootNamespace
The generator uses RootNamespace for output namespaces. If not set, assembly name is used.
xml
<PropertyGroup>
<RootNamespace>MyCompany.MyProduct</RootNamespace>
</PropertyGroup>Generated Code
csharp
// <auto-generated/>
namespace MyCompany.MyProduct;
public static class ServiceCollectionExtensions
{
// ...
}