ConfigurationKey Struct
Definition
- Namespace
- Bodu.Text.Configuration
- Assembly
- Bodu.Text.Configuration.dll
- Package
- Bodu.Text.Configuration 1.0.0
Represents a configuration key in both its raw, file-level form and its colon-delimited logical form used by the
resolved view and by Microsoft.Extensions.Configuration.
public readonly struct ConfigurationKey : IEquatable<ConfigurationKey>
- Implements
- Inherited Members
- Extension Methods
Examples
// Default mapping - split on '.' and ':', case-insensitive comparison.
var k1 = new ConfigurationKey("Logging.Level.Default");
var k2 = new ConfigurationKey("logging:level:default");
Console.WriteLine(k1 == k2); // True - same segment sequence under the default comparer
Console.WriteLine(k1.Path); // "Logging:Level:Default"
Console.WriteLine(string.Join(",", k1.Segments)); // "Logging,Level,Default"
// Case-sensitive parsing for hosts that distinguish 'Foo' from 'foo'.
var opts = new ConfigurationKeyOptions { CaseSensitive = true };
var k3 = new ConfigurationKey("Foo:Bar", opts);
var k4 = new ConfigurationKey("foo:bar", opts);
Console.WriteLine(k3 == k4); // False
Remarks
The struct stores the input RawKey verbatim. Path is the canonical colon-joined form derived from Segments, applying the mapping policy from ConfigurationKeyOptions.
Equality compares the segment sequence under the configured comparer; the raw form is informational only.
Both dotted (logging.level.default) and colon-delimited (logging:level:default) source forms produce
the same canonical Path, so consumers can mix the two notations in a document without disturbing the
resolved view. Whitespace in segments is trimmed; control characters are rejected at construction time.
Constructors
ConfigurationKey(string, ConfigurationKeyOptions?)
Initializes a new instance of the ConfigurationKey struct from a raw key string using the supplied options.
public ConfigurationKey(string rawKey, ConfigurationKeyOptions? options = null)
Parameters
rawKeystringThe raw key as authored in the configuration source.
optionsConfigurationKeyOptionsThe key options to apply, or null for the defaults.
Exceptions
- ArgumentException
rawKeyis null, empty, or contains only whitespace; or a segment was empty when empty segments are not permitted.
Properties
CaseSensitive
Gets a value indicating whether equality and hashing for this key are case-sensitive.
public bool CaseSensitive { get; }
Property Value
Path
Gets the canonical colon-delimited logical key path derived from Segments.
public string Path { get; }
Property Value
- string
The configuration key in colon-delimited form, or the empty string for a default instance.
RawKey
Gets the raw key string exactly as it appeared in the source document.
public string RawKey { get; }
Property Value
- string
The original key text, or the empty string for a default instance.
Segments
Gets the segments produced by splitting RawKey on the configured separators.
public ImmutableArray<string> Segments { get; }
Property Value
- ImmutableArray<string>
An immutable array of segment strings.
Methods
Equals(ConfigurationKey)
Determines whether this key has the same segment sequence as other under the configured
comparer.
public bool Equals(ConfigurationKey other)
Parameters
otherConfigurationKeyThe other key to compare with.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Parse(string)
Parses a raw configuration key into a ConfigurationKey using the default key options.
public static ConfigurationKey Parse(string rawKey)
Parameters
rawKeystringThe raw key to parse.
Returns
- ConfigurationKey
A ConfigurationKey built from
rawKey.
Exceptions
- ArgumentException
rawKeyis null, empty, or contains only whitespace, or contains an empty segment when empty segments are not permitted.
Parse(string, ConfigurationKeyOptions?)
Parses a raw configuration key into a ConfigurationKey using the supplied options.
public static ConfigurationKey Parse(string rawKey, ConfigurationKeyOptions? options)
Parameters
rawKeystringThe raw key to parse.
optionsConfigurationKeyOptionsThe key options to apply, or null for the defaults.
Returns
- ConfigurationKey
A ConfigurationKey built from
rawKey.
Exceptions
- ArgumentException
rawKeyis null, empty, or contains only whitespace, or contains an empty segment when empty segments are not permitted.
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
TryParse(string?, out ConfigurationKey)
Attempts to parse a raw configuration key.
public static bool TryParse(string? rawKey, out ConfigurationKey result)
Parameters
rawKeystringThe raw key to parse.
resultConfigurationKeyWhen this method returns, contains the parsed key if successful; otherwise, the default key.
Returns
TryParse(string?, ConfigurationKeyOptions?, out ConfigurationKey)
Attempts to parse a raw configuration key using the supplied options.
public static bool TryParse(string? rawKey, ConfigurationKeyOptions? options, out ConfigurationKey result)
Parameters
rawKeystringThe raw key to parse.
optionsConfigurationKeyOptionsThe key options to apply, or null for the defaults.
resultConfigurationKeyWhen this method returns, contains the parsed key if successful; otherwise, the default key.
Returns
Operators
operator ==(ConfigurationKey, ConfigurationKey)
Determines whether two keys are equal.
public static bool operator ==(ConfigurationKey left, ConfigurationKey right)
Parameters
leftConfigurationKeyThe first key to compare.
rightConfigurationKeyThe second key to compare.
Returns
operator !=(ConfigurationKey, ConfigurationKey)
Determines whether two keys are not equal.
public static bool operator !=(ConfigurationKey left, ConfigurationKey right)
Parameters
leftConfigurationKeyThe first key to compare.
rightConfigurationKeyThe second key to compare.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |