DotNetExtras.Configuration
1.0.1
dotnet add package DotNetExtras.Configuration --version 1.0.1
NuGet\Install-Package DotNetExtras.Configuration -Version 1.0.1
<PackageReference Include="DotNetExtras.Configuration" Version="1.0.1" />
<PackageVersion Include="DotNetExtras.Configuration" Version="1.0.1" />
<PackageReference Include="DotNetExtras.Configuration" />
paket add DotNetExtras.Configuration --version 1.0.1
#r "nuget: DotNetExtras.Configuration, 1.0.1"
#:package DotNetExtras.Configuration@1.0.1
#addin nuget:?package=DotNetExtras.Configuration&version=1.0.1
#tool nuget:?package=DotNetExtras.Configuration&version=1.0.1
DotNetExtras.Configuration
DotNetExtras.Configuration
is a .NET Core library that simplifies loading, reading, and transforming configuration settings of .NET applications.
Use the DotNetExtras.Configuration
library to:
- Load configuration from JSON files, JSON strings, or
Dictionary
objects (useful for mocking application setting in unit tests). - Retrieve application settings and assign them to strongly-typed variables including primitive types (
string
,int
,enum
,boolean
), arrays, collections, lists, hash sets, and dictionaries. - Set configuration once during application startup and access it from anywhere in the code.
- Reload configuration settings.
Usage
The following examples illustrate how to use the DotNetExtras.Configuration
API:
using Microsoft.Extensions.Configuration
using DotNetExtras.Configuration;
...
IConfiguration? config = null;
// Load configuration from a JSON file.
config = AppSettings.Load.FromJsonFile("C:\\AppSettings.json");
// Load configuration from a JSON string.
config = AppSettings.Load.FromJsonFile("{\"a\":{\"b\":\"c\"}}");
// Load configuration from a dictionary (keys of array elements must be indexed).
config = AppSettings.Load.FromDictionary(
new Dictionary<string,string?>
{
{"A", "ValueA"},
{"B:X", "ValueB"},
{"C:Z:0", "ValueC"},
{"C:Z:1", "ValueD"},
{"C:Z:2", "ValueE"},
}
);
// Save configuration in a static global variable.
AppSettings.Global.Set(config);
// Get configuration from the static global variable.
config = AppSettings.Global.Get();
// Reinitialize the configuration from the original provider.
AppSettings.Reload(config);
// Get a strongly-typed primitive value from the configuration.
string? a = AppSettings.GetValue<string>("KeyX:SubKeyA");
int? b = AppSettings.GetValue<int>("KeyX:SubKeyB");
bool? c = AppSettings.GetValue<bool>("KeyX:SubKeyC");
// Get a strongly typed collection value from the configuration.
string[]? d1 = AppSettings.GetArrayValue<string[]>("KeyX:SubKeyD1");
int[]? d2 = AppSettings.GetArrayValue<int[]>("KeyX:SubKeyD2"");
List<string>? e1 = AppSettings.GetListValue<List<string>>("KeyX:SubKeyE1");
List<int>? e2 = AppSettings.GetListValue<List<string>>("KeyX:SubKeyE2");
HashSet<string>? f1 = AppSettings.GetHashSetValue<string>("KeyX:SubKeyF1");
HashSet<int>? f2 = AppSettings.GetHashSetValue<string>("KeyX:SubKeyF2");
Dictionary<string, string>? g1 = AppSettings.GetDictionaryValue<string, string>("KeyX:SubKeyG1");
Dictionary<string, int>? g2 = AppSettings.GetDictionaryValue<string, int>("KeyX:SubKeyG2");
// Get a strongly typed enum value from the configuration.
MyEnum? h = AppSettings.GetEnumValue<MyEnum>("KeyX:SubKeyH");
Documentation
For complete documentation, usage details, and code samples, see:
Package
Install the latest version of the DotNetExtras.Configuration
NuGet package from:
See also
Check out other DotNetExtras
libraries at:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 9.0.8)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.8)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.8)
- Microsoft.Extensions.Configuration.Json (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fixed typo in the method name.