ConfigurationExtensions
Extension methods for configuration classes.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Microsoft.Extensions.Configuration
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class ConfigurationExtensions
{
public static IConfigurationBuilder Add<[System.Runtime.CompilerServices.Nullable(0)] TSource>(this IConfigurationBuilder builder, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Action<TSource> configureSource) where TSource : IConfigurationSource, new
{
TSource val = new TSource();
configureSource?.Invoke(val);
return builder.Add((IConfigurationSource)(object)val);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public static string GetConnectionString(this IConfiguration configuration, string name)
{
return configuration?.GetSection("ConnectionStrings")[name];
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
2
})]
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration)
{
return configuration.AsEnumerable(false);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
2
})]
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration, bool makePathsRelative)
{
Stack<IConfiguration> stack = new Stack<IConfiguration>();
stack.Push(configuration);
int num;
if (makePathsRelative) {
IConfigurationSection configurationSection = configuration as IConfigurationSection;
if (configurationSection != null) {
num = configurationSection.Path.Length + 1;
goto IL_005e;
}
}
num = 0;
goto IL_005e;
IL_005e:
int prefixLength = num;
while (stack.Count > 0) {
IConfiguration config = stack.Pop();
IConfigurationSection configurationSection2 = config as IConfigurationSection;
if (configurationSection2 != null && (!makePathsRelative || config != configuration))
yield return new KeyValuePair<string, string>(configurationSection2.Path.Substring(prefixLength), configurationSection2.Value);
foreach (IConfigurationSection child in config.GetChildren()) {
stack.Push(child);
}
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static bool Exists([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] this IConfigurationSection section)
{
if (section == null)
return false;
if (section.Value == null)
return section.GetChildren().Any();
return true;
}
public static IConfigurationSection GetRequiredSection(this IConfiguration configuration, string key)
{
System.ThrowHelper.ThrowIfNull(configuration, "configuration");
IConfigurationSection section = configuration.GetSection(key);
if (section.Exists())
return section;
throw new InvalidOperationException(System.SR.Format(System.SR.InvalidSectionName, key));
}
}
}