Table of Contents

ConfigurationView Class

Definition

Namespace
Bodu.Text.Configuration
Assembly
Bodu.Text.Configuration.dll
Package
Bodu.Text.Configuration 1.0.0
Source
ConfigurationView.Getters.cs

Represents the resolved snapshot of a configuration document for a specific target path: a flattened dictionary of configuration keys to their effective values, computed by layering preamble and matching sections in source order.

public sealed class ConfigurationView : IReadOnlyDictionary<string, string?>, IReadOnlyCollection<KeyValuePair<string, string?>>, IEnumerable<KeyValuePair<string, string?>>, IEnumerable
Inheritance
ConfigurationView
Implements
Inherited Members
Extension Methods

Examples

IniDocument           doc  = ConfigurationDocument.Parse(text);
ConfigurationView view = doc.Resolve("src/Foo.cs");

// Indexer lookup - colon and dotted forms are equivalent.
string? level = view["logging:level:default"];
string? alt   = view["logging.level.default"]; // same value

// Typed convenience accessors on the view.
int indent = view.GetInt32("format:indent:size", fallback: 4);

// Enumeration yields canonical colon-delimited keys.
foreach (KeyValuePair<string, string?> kv in view)
    Console.WriteLine($"{kv.Key} = {kv.Value}");

Remarks

Use Resolve(IniDocumentBase, string?, ConfigurationResolveOptions?) to obtain a view for a target path. The view is a one-shot snapshot - subsequent mutation of the originating document does not retroactively update the view.

Values are string? to match Microsoft.Extensions.Configuration's IReadOnlyDictionary<TKey, TValue> shape. A key that resolves to the EditorConfig sentinel unset under RemoveEffectiveValue is omitted entirely.

Lookups accept either the canonical colon-delimited form (logging:level:default) or the dotted form ( logging.level.default); both resolve to the same value because dotted keys are normalized to colon-delimited form before consulting the backing dictionary. Enumeration yields keys in their canonical colon-delimited form.

Properties

Count

Gets the number of resolved keys.

public int Count { get; }

Property Value

int

The count of keys present in the resolved view.

Entries

Gets the resolved-entry origin metadata for every key in the view.

public IEnumerable<ConfigurationResolvedEntry> Entries { get; }

Property Value

IEnumerable<ConfigurationResolvedEntry>

Origin metadata per resolved key.

this[string]

Gets the effective value for key, or null if the key is absent from the resolved view.

public string? this[string key] { get; }

Parameters

key string

The configuration key, in colon-delimited form (e.g. logging:level:default) or the dotted form ( logging.level.default). Both produce the same lookup.

Property Value

string

The value, or null when absent.

Remarks

This indexer deliberately returns null for an absent key rather than throwing KeyNotFoundException as this[TKey] normally would, matching Microsoft.Extensions.Configuration's null-on-absent convention. Use ContainsKey(string) to distinguish an absent key from one whose value is null.

Exceptions

ArgumentNullException

key is null.

Keys

Gets the configuration keys present in the resolved view.

public IEnumerable<string> Keys { get; }

Property Value

IEnumerable<string>

An enumerable of configuration keys.

Values

Gets the underlying resolved dictionary as a read-only view.

public IReadOnlyDictionary<string, string?> Values { get; }

Property Value

IReadOnlyDictionary<string, string>

The resolved values keyed by configuration key.

Methods

ContainsKey(string)

Determines whether the resolved view contains key, accepting the key in any equivalent separator notation.

public bool ContainsKey(string key)

Parameters

key string

The configuration key, in any accepted separator notation.

Returns

bool

true when the key is present; otherwise, false.

Exceptions

ArgumentNullException

key is null.

GetBoolean(string)

Gets the boolean value for key using EditorConfig conventions (true or false, case-insensitive).

public bool GetBoolean(string key)

Parameters

key string

The configuration key.

Returns

bool

The parsed boolean value.

Remarks

Accepted: true, True, TRUE, false, False, FALSE and any other case variation. Surrounding whitespace is tolerated by the underlying TryParse(string, out bool).

Rejected: yes, no, on, off, 1, 0. EditorConfig 0.17.2 uses only the true/false literals; broadening the set would conflict with the spec. Callers who want a relaxed parser should write their own using GetString(string).

Exceptions

KeyNotFoundException

The key is absent.

FormatException

The value cannot be parsed.

GetBoolean(string, bool)

Gets the boolean value for key, returning fallback on missing keys.

public bool GetBoolean(string key, bool fallback)

Parameters

key string

The configuration key.

fallback bool

The value to return when the key is absent.

Returns

bool

The parsed value or fallback.

GetEntry(string)

Gets the origin metadata for key, or null when the key is absent from the resolved view.

public ConfigurationResolvedEntry? GetEntry(string key)

Parameters

key string

The configuration key in either dotted or colon-delimited form.

Returns

ConfigurationResolvedEntry

The resolved entry, or null when absent.

Exceptions

ArgumentNullException

key is null.

GetEnum<TEnum>(string)

Gets the value for key as an enum of type TEnum.

public TEnum GetEnum<TEnum>(string key) where TEnum : struct, Enum

Parameters

key string

The configuration key.

Returns

TEnum

The parsed enum value.

Type Parameters

TEnum

The enum type.

Remarks

Enum names are parsed case-insensitively. Numeric values are accepted only when they correspond to a declared enum member - undefined integers (e.g. severity = 99 for a three-member enum) are rejected with FormatException rather than producing a synthetic value.

Combined values for FlagsAttribute-decorated enums are also rejected unless the combined value itself is a declared member - the underlying IsDefined(Type, object) guard treats Read, Write as undefined when only the individual flags are declared. Callers who need combined-flag parsing should call Parse(Type, string, bool) against GetString(string) instead.

ConfigurationView view = ConfigurationDocument.Parse(text).Resolve("src/Foo.cs");

// Names parse case-insensitively; undefined integers are rejected.
LogLevel level = view.GetEnum<LogLevel>("logging:level");

// Fall back when the key is absent (a malformed value still throws).
LogLevel effective = view.GetEnum("logging:level", LogLevel.Information);

Exceptions

KeyNotFoundException

The key is absent.

FormatException

The value cannot be parsed as the enum.

GetEnum<TEnum>(string, TEnum)

Gets the value for key as an enum of type TEnum, returning fallback when the key is absent. Present-but-malformed values still throw FormatException.

public TEnum GetEnum<TEnum>(string key, TEnum fallback) where TEnum : struct, Enum

Parameters

key string

The configuration key.

fallback TEnum

The value to return when the key is absent.

Returns

TEnum

The parsed enum value, or fallback when the key is absent.

Type Parameters

TEnum

The enum type.

Exceptions

ArgumentNullException

key is null.

FormatException

The value is present but cannot be parsed as TEnum.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<KeyValuePair<string, string?>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, string>>

An enumerator that can be used to iterate through the collection.

GetInt32(string)

Gets the 32-bit integer value for key, throwing on missing or malformed values.

public int GetInt32(string key)

Parameters

key string

The configuration key.

Returns

int

The parsed integer value.

Exceptions

KeyNotFoundException

The key is absent.

FormatException

The value cannot be parsed as an integer.

GetInt32(string, int)

Gets the 32-bit integer value for key, returning fallback on missing keys. Present-but-malformed values still throw FormatException.

public int GetInt32(string key, int fallback)

Parameters

key string

The configuration key.

fallback int

The value to return when the key is absent.

Returns

int

The parsed value or fallback.

GetInt64(string)

Gets the 64-bit integer value for key.

public long GetInt64(string key)

Parameters

key string

The configuration key.

Returns

long

The parsed value.

Exceptions

KeyNotFoundException

The key is absent.

FormatException

The value cannot be parsed.

GetInt64(string, long)

Gets the 64-bit integer value for key, returning fallback on missing keys. Present-but-malformed values still throw FormatException.

public long GetInt64(string key, long fallback)

Parameters

key string

The configuration key.

fallback long

The value to return when the key is absent.

Returns

long

The parsed value or fallback.

Exceptions

ArgumentNullException

key is null.

GetString(string)

Gets the raw string value for key, throwing if the key is missing.

public string GetString(string key)

Parameters

key string

The configuration key in colon-delimited form.

Returns

string

The value as authored.

Exceptions

ArgumentNullException

key is null.

KeyNotFoundException

The key is absent from the resolved view.

GetString(string, string?)

Gets the string value for key, returning fallback when absent.

public string? GetString(string key, string? fallback)

Parameters

key string

The configuration key in colon-delimited form.

fallback string

The value to return when the key is absent.

Returns

string

The resolved value or fallback.

Exceptions

ArgumentNullException

key is null.

GetValue<T>(string)

Gets the value for key parsed as T using InvariantCulture. Mirrors IniSection.GetValue<T>(key).

public T GetValue<T>(string key) where T : ISpanParsable<T>

Parameters

key string

The configuration key, in either dotted or colon-delimited form.

Returns

T

The parsed value.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Examples

ConfigurationView view = ConfigurationDocument.Parse(text).Resolve("src/Foo.cs");

// Parse any ISpanParsable<T> directly from the resolved view.
int     port    = view.GetValue<int>("server:port");
double  ratio   = view.GetValue<double>("cache:fill:ratio");
Guid    tenant  = view.GetValue<Guid>("tenant:id");

Exceptions

ArgumentNullException

key is null.

KeyNotFoundException

The key is absent from the resolved view.

FormatException

The value cannot be parsed as T.

GetValue<T>(string, T)

Gets the value for key parsed as T, returning fallback when the key is absent. Present-but-malformed values still throw.

public T GetValue<T>(string key, T fallback) where T : ISpanParsable<T>

Parameters

key string

The configuration key, in either dotted or colon-delimited form.

fallback T

The value to return when the key is absent.

Returns

T

The parsed value, or fallback when the key is absent.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Exceptions

ArgumentNullException

key is null.

FormatException

The value is present but cannot be parsed as T.

TryGetBoolean(string, out bool)

Attempts to parse the value for key as a boolean.

public bool TryGetBoolean(string key, out bool value)

Parameters

key string

The configuration key.

value bool

When this method returns, contains the parsed value; otherwise, false.

Returns

bool

true when the value was present and parseable.

TryGetEnum<TEnum>(string, out TEnum)

Attempts to parse the value for key as an enum of type TEnum.

public bool TryGetEnum<TEnum>(string key, out TEnum value) where TEnum : struct, Enum

Parameters

key string

The configuration key.

value TEnum

When this method returns, contains the parsed value; otherwise, the default.

Returns

bool

true when the value was present and parsed to a declared member; otherwise, false.

Type Parameters

TEnum

The enum type.

Exceptions

ArgumentNullException

key is null.

TryGetInt32(string, out int)

Attempts to parse the value for key as a 32-bit integer.

public bool TryGetInt32(string key, out int value)

Parameters

key string

The configuration key.

value int

When this method returns, contains the parsed value; otherwise, zero.

Returns

bool

true when the value was present and parseable; otherwise, false.

TryGetInt64(string, out long)

Attempts to parse the value for key as a 64-bit integer.

public bool TryGetInt64(string key, out long value)

Parameters

key string

The configuration key.

value long

When this method returns, contains the parsed value; otherwise, zero.

Returns

bool

true when the value was present and parseable; otherwise, false.

Exceptions

ArgumentNullException

key is null.

TryGetString(string, out string?)

Attempts to get the string value for key without throwing.

public bool TryGetString(string key, out string? value)

Parameters

key string

The configuration key.

value string

When this method returns, contains the value if found; otherwise, null.

Returns

bool

true when the key was present; otherwise, false.

TryGetValue<T>(string, out T)

Attempts to get the value for key parsed as T, returning false on missing or malformed values. Never throws on a parse failure.

public bool TryGetValue<T>(string key, out T value) where T : ISpanParsable<T>

Parameters

key string

The configuration key, in either dotted or colon-delimited form.

value T

When this method returns true, contains the parsed value.

Returns

bool

true when the value was present and parseable; otherwise, false.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Exceptions

ArgumentNullException

key is null.

Explicit Interface Implementations

IReadOnlyDictionary<string, string>.TryGetValue(string, out string)

Attempts to get the value for key without throwing, accepting either the colon-delimited or the equivalent dotted form. Satisfies TryGetValue(TKey, out TValue).

bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)

Parameters

key string

The configuration key, in either dotted or colon-delimited form.

value string

When this method returns true, contains the resolved value.

Returns

bool

true when the key is present; otherwise, false.

Exceptions

ArgumentNullException

key is null.

IReadOnlyDictionary<string, string>.Values

Gets the values present in the resolved view. Satisfies Values.

IEnumerable<string?> IReadOnlyDictionary<string, string>.Values { get; }

Returns

IEnumerable<string>

An enumerable of the resolved values.

IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator

An IEnumerator object that can be used to iterate through the collection.

Applies to

ProductVersions
.NET8, 10