Generate strongly-typed settings classes from appsettings.json at compile time.
No more hand-writing settings classes, registering IOptions<T> bindings, or keeping C# code in sync with JSON config. AppSettingsGen reads your appsettings.json during compilation and generates matching classes and DI registration code automatically.
- Type inference — Infers
string,int,long,double,bool, arrays, and nested objects from JSON values - Class generation — Generates a
Settingsclass per JSON section with PascalCase properties - DI registration — Generates
AddAppSettings(IConfiguration)extension method forIOptions<T>pattern - AOT compatible — All generated code is static with no reflection; works with NativeAOT trimming
- Incremental — Only regenerates when
appsettings.jsonchanges (Roslyn incremental source generator) - Naming conventions — Converts
camelCase,snake_case, andkebab-caseJSON keys to PascalCase C# properties
dotnet add package AppSettingsGenIn your .csproj:
<ItemGroup>
<AdditionalFiles Include="appsettings.json" />
</ItemGroup>{
"Database": {
"ConnectionString": "Server=localhost;Database=MyDb",
"MaxRetries": 3
},
"Logging": {
"Level": "Information"
}
}The generator produces classes in the AppSettingsGen.Generated namespace:
using AppSettingsGen.Generated;
using Microsoft.Extensions.Options;
// Register all settings in one call
builder.Services.AddAppSettings(builder.Configuration);
// Inject via IOptions<T>
public class MyService
{
public MyService(IOptions<DatabaseSettings> dbOptions)
{
var connectionString = dbOptions.Value.ConnectionString;
var maxRetries = dbOptions.Value.MaxRetries;
}
}// Auto-generated from appsettings.json
namespace AppSettingsGen.Generated
{
public partial class DatabaseSettings
{
public string? ConnectionString { get; init; }
public int MaxRetries { get; init; }
}
public partial class LoggingSettings
{
public string? Level { get; init; }
}
}| JSON Value | C# Type |
|---|---|
"text" |
string? |
42 |
int |
9999999999 |
long |
3.14 |
double |
true / false |
bool |
["a", "b"] |
List<string>? |
[1, 2] |
List<int>? |
{ ... } |
Nested class |
[{ ... }] |
List<ItemClass>? |
Nested JSON objects become nested C# classes:
{
"App": {
"Cache": {
"Enabled": true,
"TtlSeconds": 300
}
}
}Generates:
public partial class AppSettings
{
public Cache? Cache { get; init; }
public partial class Cache
{
public bool Enabled { get; init; }
public int TtlSeconds { get; init; }
}
}Arrays of objects generate a typed item class:
{
"Config": {
"Endpoints": [
{ "Url": "http://api.example.com", "Timeout": 30 }
]
}
}Generates:
public partial class ConfigSettings
{
public List<EndpointsItem>? Endpoints { get; init; }
public partial class EndpointsItem
{
public string? Url { get; init; }
public int Timeout { get; init; }
}
}| Code | Severity | Description |
|---|---|---|
| ASG001 | Error | Invalid JSON in appsettings file |
| ASG002 | Warning | No object sections found to generate |
- .NET 8.0+ (for the consuming project)
- C# 12+
- Roslyn 4.12+
- Fork the repository
- Create a feature branch
- Write tests for your changes
- Run
dotnet testanddotnet format - Submit a pull request
MIT