CsConfUtil 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CsConfUtil --version 1.0.0
                    
NuGet\Install-Package CsConfUtil -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CsConfUtil" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CsConfUtil" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CsConfUtil" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CsConfUtil --version 1.0.0
                    
#r "nuget: CsConfUtil, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=CsConfUtil&version=1.0.0
                    
Install CsConfUtil as a Cake Addin
#tool nuget:?package=CsConfUtil&version=1.0.0
                    
Install CsConfUtil as a Cake Tool

CsConfigUtil

CsConfigUtil is a lightweight utility library for extending the functionality of IConfiguration in .NET applications. It simplifies retrieving configuration sections as strongly typed objects, with support for required sections and default naming conventions.

Features

  • Retrieve configuration sections as strongly typed objects.
  • Specify custom section names or use default naming based on the type.
  • Exception handling for missing required configuration sections.

Installation

Currently not on nuget.org, clone this repo and reference project.

Usage

Basic Usage

  1. Add a configuration source to your IConfiguration instance.
  2. Use the extension methods GetAs<T> or GetRequiredAs<T> to retrieve configuration sections.

Examples

Example Configuration

Assume the following JSON configuration:

{  
  "MyClass": {  
    "Property": "Value"  
  },  
  "AnotherClass": {  
    "Property": "AnotherValue"  
  }  
}
Setting Up Configuration
using Microsoft.Extensions.Configuration;
using CsConfigUtil;

var configurationBuilder = new ConfigurationBuilder();  
configurationBuilder.AddJsonFile("appsettings.json");  
var configuration = configurationBuilder.Build();
Strongly Typed Access
public class MyClass  
{  
    public string Property { get; set; } = string.Empty;  
}

// Retrieve configuration section as a strongly typed object  
var myClass = configuration.GetAs<MyClass>();  
if (myClass != null)  
{  
    Console.WriteLine(myClass.Property); // Output: Value  
}
Handling Required Sections
try  
{  
    var requiredClass = configuration.GetRequiredAs<MyClass>();  
    Console.WriteLine(requiredClass.Property); // Output: Value  
}  
catch (ArgumentException ex)  
{  
    Console.WriteLine(ex.Message);  
}

Custom Section Names

By default, GetAs<T> and GetRequiredAs<T> use the type name as the section name. You can specify a custom name if needed:

var anotherClass = configuration.GetAs<MyClass>("AnotherClass");  
if (anotherClass != null)  
{  
    Console.WriteLine(anotherClass.Property); // Output: AnotherValue  
}

Target Framework Compatibility

This library targets .NET 8.0 and .NET 9.0 to provide broad compatibility and support modern features. The net8.0 build ensures functionality on .NET 8.0 while the net9.0 build allows access to APIs and optimizations available in .NET 9.0. For earlier frameworks you may clone the repository and adapt the code as needed, as the library is simple and easy to integrate.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Changelog

See full version history in CHANGELOG.md

Product 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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1 155 3/9/2025
1.0.0 154 3/9/2025