Table of Contents

Class PropertyExt

Namespace
Tlabs.Misc
Assembly
Tlabs.Core.dll

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 string
defaultVal bool

Returns

bool

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 string
defaultVal int

Returns

int

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 string
defaultVal object

Returns

object

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

string

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 string
defaultVal string

Returns

string

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 string

A 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, the propSpecifier 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 string

properties key path (using '.' as path delimiter)

val object

value to be set

resolvedKey string

resolved key (last token of the path on success)

pathSep char

Optional 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?>)

public static IReadOnlyDictionary<string, object?> ToReadonly(this IDictionary<string, object?> prop)

Parameters

prop IDictionary<string, object>

Returns

IReadOnlyDictionary<string, object>

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 string

properties key path (using pathSep as path separator)

val object

resolved value

resolvedKey string

resolved key

pathSep char

Optional 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