Class PropertyExt
Extension methods for accessing properties of a IDictionary<TKey, TValue> with K is string and T is object.
public static class PropertyExt
- Inheritance
-
PropertyExt
- Inherited Members
Methods
GetBool(IDictionary<string, object?>, string, bool)
Return a property's boolean value or defaultVal
if not existing or not convertible to bool.
public static bool GetBool(this IDictionary<string, object?> prop, string propKey, bool defaultVal)
Parameters
prop
IDictionary<string, object>propKey
stringdefaultVal
bool
Returns
GetInt(IDictionary<string, object?>, string, int)
Return a property's integer value or defaultVal
if not existing or not convertible to int.
public static int GetInt(this IDictionary<string, object?> prop, string propKey, int defaultVal)
Parameters
prop
IDictionary<string, object>propKey
stringdefaultVal
int
Returns
GetOrSet(IDictionary<string, object?>, string, object?)
Return a property's value or defaultVal
if not existing - in that case is also set as new properties value.
public static object? GetOrSet(this IDictionary<string, object?> prop, string propKey, object? defaultVal)
Parameters
prop
IDictionary<string, object>propKey
stringdefaultVal
object
Returns
GetString(IDictionary<string, object?>, string)
Return a property's string value or null if not existing or not a string.
public static string? GetString(this IDictionary<string, object?> prop, string propKey)
Parameters
prop
IDictionary<string, object>propKey
string
Returns
GetString(IDictionary<string, object?>, string, string?)
Return a property's string value or defaultVal
if not existing or not a string.
public static string? GetString(this IDictionary<string, object?> prop, string propKey, string? defaultVal)
Parameters
prop
IDictionary<string, object>propKey
stringdefaultVal
string
Returns
ResolvedProperty(IDictionary<string, object?>, string)
Resolved property value.
public static object ResolvedProperty(this IDictionary<string, object?> prop, string propSpecifier)
Parameters
prop
IDictionary<string, object>A properties dictionary
propSpecifier
stringA property spcifier. If it is a name or property path enclosed in brackets like '[name.subKey]', the contents of the bracket are tried to be resolved with TryResolveValue(IDictionary<string, object?>, string, out object, out string?, char)
Returns
- object
The resolved property value given by the
propSpecifier
or if could not be resolved as a property, thepropSpecifier
it self.
SetResolvedValue(IDictionary<string, object?>, string, object?, out string?, char)
Set val
to resolved (optionaly) nested properties dictionary.
public static bool SetResolvedValue(this IDictionary<string, object?> prop, string propKeyPath, object? val, out string? resolvedKey, char pathSep = '.')
Parameters
prop
IDictionary<string, object>(optionaly nested) properties dictionary
propKeyPath
stringproperties key path (using '.' as path delimiter)
val
objectvalue to be set
resolvedKey
stringresolved key (last token of the path on success)
pathSep
charOptional path seperator char (defaults to '.')
Returns
- bool
true if value could be set
Remarks
Assuming a propKeyPath
of "p1.p2.p3"
. This would be (tried)
to be resolved like: properties["p1"]["p2"]["p3"] = val
,
Every but the last tokens of the propKeyPath
that could not be resolved into a dictionary value
gets created as a new dictionary, if not already existing. If it exists, but is not of dictionary type - false is returned.
ToReadonly(IDictionary<string, object?>)
Convert IReadOnlyDictionary<TKey, TValue> into read-only IDictionary<TKey, TValue>.
public static IReadOnlyDictionary<string, object?> ToReadonly(this IDictionary<string, object?> prop)
Parameters
prop
IDictionary<string, object>
Returns
TryResolveValue(IDictionary<string, object?>, string, out object, out string?, char)
Tries to resolve a value from (optionaly) nested properties dictionaries.
public static bool TryResolveValue(this IDictionary<string, object?> prop, string propKeyPath, out object val, out string? resolvedKey, char pathSep = '.')
Parameters
prop
IDictionary<string, object>(optionaly nested) properties dictionary
propKeyPath
stringproperties key path (using
pathSep
as path separator)val
objectresolved value
resolvedKey
stringresolved key
pathSep
charOptional path seperator char (defaults to '.')
Returns
- bool
true if a value could be resolved using the
propKeyPath
Remarks
Assuming a propKeyPath
of "p1.p2.p3"
. This would be (tried)
to be resolved like: properties["p1"]["p2"]["p3"]
,
The first key token of the propKeyPath
that is associated with a non dictionary value (or the last)
is returned in resolvedKey