SourceGenerator.Configuration
1.0.0
dotnet add package SourceGenerator.Configuration --version 1.0.0
NuGet\Install-Package SourceGenerator.Configuration -Version 1.0.0
<PackageReference Include="SourceGenerator.Configuration" Version="1.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="SourceGenerator.Configuration" Version="1.0.0" />
<PackageReference Include="SourceGenerator.Configuration"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add SourceGenerator.Configuration --version 1.0.0
#r "nuget: SourceGenerator.Configuration, 1.0.0"
#:package SourceGenerator.Configuration@1.0.0
#addin nuget:?package=SourceGenerator.Configuration&version=1.0.0
#tool nuget:?package=SourceGenerator.Configuration&version=1.0.0
ConfigurationGenerator
A helper Source Generator, to generate a strongly typed configuration from a json file.
It is meant to be used with the Microsoft.Extensions.Configuration library and its IConfiguration interface.
Getting started
Define your class that will hold the configuration:
[SourceGenerator.Configuration.GenerateConfigurationPropertiesAttribute("config-definition.json", nameof(configuration))]
public partial class MyConfiguration(IConfiguration configuration)
{
}
The GenerateConfigurationAccessors attribute will generate the properties for the configuration class based on the json file config-definition.json.
Ensure that the class is partial and that it can access the IConfiguration instance. The Sample uses the dependency injection to inject the IConfiguration instance.
But you could also MySingelton.Configuration instead of nameof(configuration) to access the static property of MySingleton class.
The config-definition.json file could look like this:
{
"some-int-value": {
"type": "int"
},
"some-required-string": {
"type": "string",
"required": true
},
"some-nested-properties": {
"type": "namespace",
"description": "Some nested Property",
"members": {
"nested-int": {
"type": "int",
"default": 42
}
}
}
}
The JSON must have the structure defined in the json schema and is hopfully self-explanatory with this example.
The configuration must be marked in the project file as AdditionalFiles:
<ItemGroup>
<AdditionalFiles Include="config-definition.json" />
</ItemGroup>
The path in the Project is not important, only the name will be matched. This means also that the file name must be unique in the project.
The generated class will look like this (uglier formated…):
#nullable restore
using System;
namespace MyNamespcae
{
internal partial class MyConfiguration : MyConfiguration.ISomeNestedProperties
{
/// <summary>Some nested Property
/// </summary>
public ISomeNestedProperties SomeNestedProperties => (this as ISomeNestedProperties)!;
/// <summary>Some nested Property
/// </summary>
public interface ISomeNestedProperties
{
int NestedInt { get; }
}
public int? SomeIntValue => config.GetValue<int?>("some-int-value");
public string SomeRequiredString => config.GetValue<string?>("some-required-string") ?? throw new global::SourceGenerator.Configuration.MissingConfigurationException(":some-required-string");
int MyConfiguration.ISomeNestedProperties.NestedInt => config.GetValue<int?>("some-nested-properties:nested-int") ?? 42;
}
}
Todo
- Add support to check for missing required properties at load time
- Generation of JSON schema
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 373 | 3/17/2025 |
| 0.5.1 | 176 | 3/17/2025 |
| 0.5.0 | 176 | 3/17/2025 |
| 0.4.11 | 128 | 3/14/2025 |
| 0.4.10 | 117 | 3/14/2025 |
| 0.4.9 | 124 | 3/14/2025 |
| 0.4.8 | 121 | 3/14/2025 |
| 0.4.7 | 121 | 3/14/2025 |
| 0.4.6 | 123 | 3/14/2025 |
| 0.4.5 | 120 | 3/14/2025 |
| 0.4.4 | 126 | 3/14/2025 |
| 0.4.3 | 118 | 3/14/2025 |
| 0.4.2 | 121 | 3/14/2025 |
| 0.4.1 | 123 | 3/14/2025 |
| 0.4.0 | 129 | 3/14/2025 |
| 0.3.0 | 259 | 3/4/2025 |
| 0.2.0 | 263 | 3/4/2025 |
| 0.1.0 | 242 | 3/3/2025 |