ConfigurationSection
Represents a section of application configuration values.
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Microsoft.Extensions.Configuration
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[DebuggerDisplay("{DebuggerToString(),nq}")]
[DebuggerTypeProxy(typeof(ConfigurationSectionDebugView))]
public class ConfigurationSection : IConfigurationSection, IConfiguration
{
private sealed class ConfigurationSectionDebugView
{
private readonly ConfigurationSection _current;
private readonly IConfigurationProvider _provider;
public string Path => _current.Path;
public string Key => _current.Key;
public string Value => _current.Value;
public IConfigurationProvider Provider => _provider;
public List<Microsoft.Extensions.Configuration.ConfigurationSectionDebugView> Sections => Microsoft.Extensions.Configuration.ConfigurationSectionDebugView.FromConfiguration(_current, _current._root);
public ConfigurationSectionDebugView(ConfigurationSection current)
{
_current = current;
_provider = Microsoft.Extensions.Configuration.ConfigurationSectionDebugView.GetValueProvider(_current._root, _current.Path);
}
}
private readonly IConfigurationRoot _root;
private readonly string _path;
private string _key;
public string Path => _path;
public string Key => _key ?? (_key = ConfigurationPath.GetSectionKey(_path));
[System.Runtime.CompilerServices.Nullable(2)]
public string Value {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _root[Path];
}
[System.Runtime.CompilerServices.NullableContext(2)]
set {
_root[Path] = value;
}
}
[System.Runtime.CompilerServices.Nullable(2)]
public string this[string key] {
[return: System.Runtime.CompilerServices.Nullable(2)]
get {
return _root[Path + ConfigurationPath.KeyDelimiter + key];
}
[param: System.Runtime.CompilerServices.Nullable(2)]
set {
_root[Path + ConfigurationPath.KeyDelimiter + key] = value;
}
}
public ConfigurationSection(IConfigurationRoot root, string path)
{
ExceptionPolyfills.ThrowIfNull(root, "root");
ExceptionPolyfills.ThrowIfNull(path, "path");
_root = root;
_path = path;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public bool TryGetValue(string key, out string value)
{
string key2 = (key == null) ? Path : (Path + ConfigurationPath.KeyDelimiter + key);
if (_root.TryGetConfiguration(key2, out value))
return true;
value = null;
return false;
}
public IConfigurationSection GetSection(string key)
{
return _root.GetSection(Path + ConfigurationPath.KeyDelimiter + key);
}
public IEnumerable<IConfigurationSection> GetChildren()
{
return _root.GetChildrenImplementation(Path);
}
public IChangeToken GetReloadToken()
{
return _root.GetReloadToken();
}
private string DebuggerToString()
{
string text = "Path = " + Path;
int count = Microsoft.Extensions.Configuration.ConfigurationSectionDebugView.FromConfiguration(this, _root).Count;
if (count > 0)
text += $"""{count}";
if (Value != null) {
text = text + ", Value = " + Value;
IConfigurationProvider valueProvider = Microsoft.Extensions.Configuration.ConfigurationSectionDebugView.GetValueProvider(_root, Path);
if (valueProvider != null)
text += $"""{valueProvider}";
}
return text;
}
}
}