using System.Reflection;
using Config.Net.Stores;
using System.Collections.Generic;
using Config.Net.Stores.Impl.CommandLine;
namespace Config.Net
{
///
/// Configuration extensions
///
public static class ConfigurationExtensions
{
///
/// In-memory dictionary. Optionally you can pass pre-created dictionary, otherwise it will be created internally as empty.
///
public static ConfigurationBuilder UseInMemoryDictionary(
this ConfigurationBuilder builder,
IDictionary? container = null) where TInterface : class
{
builder.UseConfigStore(new DictionaryConfigStore(container));
return builder;
}
///
/// Standard app.config (web.config) builder store. Read-only.
///
public static ConfigurationBuilder UseAppConfig(this ConfigurationBuilder builder) where TInterface : class
{
builder.UseConfigStore(new AppConfigStore());
return builder;
}
///
/// Reads builder from the .dll.config or .exe.config file.
///
///
/// Reference to the assembly to look for
///
public static ConfigurationBuilder UseAssemblyConfig(this ConfigurationBuilder builder, Assembly assembly) where TInterface : class
{
builder.UseConfigStore(new AssemblyConfigStore(assembly));
return builder;
}
///
/// Uses system environment variables
///
public static ConfigurationBuilder UseEnvironmentVariables(this ConfigurationBuilder builder) where TInterface : class
{
builder.UseConfigStore(new EnvironmentVariablesStore());
return builder;
}
///
/// Simple INI storage.
///
///
/// File does not have to exist, however it will be created as soon as you try to write to it.
/// When true, inline comments are parsed. It is set to false by default so inline comments are considered a part of the value.
///
public static ConfigurationBuilder UseIniFile(this ConfigurationBuilder builder,
string iniFilePath,
bool parseInlineComments = false) where TInterface : class
{
builder.UseConfigStore(new IniFileConfigStore(iniFilePath, true, parseInlineComments));
return builder;
}
///
/// Simple INI storage.
///
///
/// File contents
/// When true, inline comments are parsed. It is set to false by default so inline comments are considered a part of the value
///
public static ConfigurationBuilder UseIniString(this ConfigurationBuilder builder,
string iniString,
bool parseInlineComments = false) where TInterface : class
{
builder.UseConfigStore(new IniFileConfigStore(iniString, false, parseInlineComments));
return builder;
}
///
/// Accepts builder from the command line arguments. This is not intended to replace a command line parsing framework but rather
/// complement it in a builder like way. Uses current process' command line parameters automatically
///
/// Configuration object
/// When true argument names are case sensitive, false by default
/// Changed builder
public static ConfigurationBuilder UseCommandLineArgs(this ConfigurationBuilder builder,
bool isCaseSensitive = false,
params KeyValuePair[] parameterNameToPosition)
where TInterface : class
{
builder.UseConfigStore(new CommandLineConfigStore(null, isCaseSensitive, parameterNameToPosition));
return builder;
}
public static ConfigurationBuilder UseCommandLineArgs(this ConfigurationBuilder builder,
bool isCaseSensitive = false,
string[]? args = null,
params KeyValuePair[] parameterNameToPosition)
where TInterface : class
{
builder.UseConfigStore(new CommandLineConfigStore(args, isCaseSensitive, parameterNameToPosition));
return builder;
}
public static ConfigurationBuilder UseCommandLineArgs(this ConfigurationBuilder builder,
params KeyValuePair[] parameterNameToPosition)
where TInterface : class
{
builder.UseConfigStore(new CommandLineConfigStore(null, false, parameterNameToPosition));
return builder;
}
///
/// Uses JSON file as a builder storage.
///
/// Configuration object.
/// Full path to json storage file.
/// Changed builder.
/// Storage file does not have to exist, however it will be created as soon as first write performed.
public static ConfigurationBuilder UseJsonFile(this ConfigurationBuilder builder, string jsonFilePath) where TInterface : class
{
builder.UseConfigStore(new JsonConfigStore(jsonFilePath, true));
return builder;
}
///
/// Uses JSON file as a builder storage.
///
/// Configuration object.
/// Json document.
/// Changed builder.
/// Storage file does not have to exist, however it will be created as soon as first write performed.
public static ConfigurationBuilder UseJsonString(this ConfigurationBuilder builder, string jsonString) where TInterface : class
{
builder.UseConfigStore(new JsonConfigStore(jsonString, false));
return builder;
}
}
}