ConfigurationSectionDebugView
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Microsoft.Extensions.Configuration
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class ConfigurationSectionDebugView
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IConfigurationSection _section;
public string Path { get; }
public string Key => _section.Key;
public string FullPath => _section.Path;
[System.Runtime.CompilerServices.Nullable(2)]
public string Value {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _section.Value;
}
}
[System.Runtime.CompilerServices.Nullable(2)]
public IConfigurationProvider Provider {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
public ConfigurationSectionDebugView(IConfigurationSection section, string path, [System.Runtime.CompilerServices.Nullable(2)] IConfigurationProvider provider)
{
_section = section;
Path = path;
Provider = provider;
}
public override string ToString()
{
string text = "Path = " + Path;
if (Value != null)
text = text + ", Value = " + Value;
if (Provider != null)
text += $"""{Provider}";
return text;
}
internal static List<ConfigurationSectionDebugView> FromConfiguration(IConfiguration current, IConfigurationRoot root)
{
List<ConfigurationSectionDebugView> list = new List<ConfigurationSectionDebugView>();
Stack<IConfiguration> stack = new Stack<IConfiguration>();
stack.Push(current);
IConfigurationSection configurationSection = current as IConfigurationSection;
int startIndex = (configurationSection != null) ? (configurationSection.Path.Length + 1) : 0;
while (stack.Count > 0) {
IConfiguration configuration = stack.Pop();
IConfigurationSection configurationSection2 = configuration as IConfigurationSection;
if (configurationSection2 != null && configuration != current) {
IConfigurationProvider valueProvider = GetValueProvider(root, configurationSection2.Path);
string path = configurationSection2.Path.Substring(startIndex);
list.Add(new ConfigurationSectionDebugView(configurationSection2, path, valueProvider));
}
foreach (IConfigurationSection child in configuration.GetChildren()) {
stack.Push(child);
}
}
list.Sort((ConfigurationSectionDebugView i1, ConfigurationSectionDebugView i2) => ConfigurationKeyComparer.Instance.Compare(i1.Path, i2.Path));
return list;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
internal static IConfigurationProvider GetValueProvider(IConfigurationRoot root, string key)
{
foreach (IConfigurationProvider item in root.Providers.Reverse()) {
if (item.TryGet(key, out string _))
return item;
}
return null;
}
}
}