Description
The StringValueMap class allows you to create a cross-language implementation of a map (dictionary) where all keys and values are strings.
Important points
- The class provides several methods to convert the stored values to different types, such as array, bool or datetime.
- This class is widely used in Pip.Services as a basis for a variety of classes, such as ConfigParams, ConnectionParams, CredentialParams (in the Pip.Services components package) and others.
Constructors
Creates a new instance of the map and assigns its value.
public
StringValueMap(IDictionary<string, string> map)
- map: IDictionary<string, string> - (optional) values to initialize this map.
Creates a new instance of the map and assigns its value.
public
StringValueMap(object map)
- map: object - (optional) values to initialize this map.
Creates a new instance of the map and assigns its value.
public
StringValueMap()
Instance methods
Append
Appends new elements to this map.
public
void Append(IDictionary<\string, object> map)
- map: IDictionary<\string, object> - map with elements to be added.
Append
Appends new elements to this map.
public
void Append(IDictionary<\string, string> map)
- map: IDictionary<\string, string> - map with elements to be added.
Clone
Creates a binary clone of this object.
public
object Clone()
- returns: object - clone of this object.
Get
Gets a map element specified by its key.
public virtual
string Get(string key)
- key: string - key of the element to get.
- returns: string - value of the map element.
GetAsArray
Converts a map element into an AnyValueArray or returns an empty AnyValueArray if the conversion is not possible.
See AnyValueArray, AnyValueArray.fromValue
public
AnyValueArray GetAsArray(string key)
- key: string - key of the element to get.
- returns: AnyValueArray - AnyValueArray value of the element or an empty AnyValueArray if the conversion is not supported.
GetAsArrayWithDefault
Converts a map element into an AnyValueArray or returns a given default value if the conversion is not possible.
See AnyValueArray
public
AnyValueArray GetAsArrayWithDefault(string key, AnyValueArray defaultValue)
- key: string - key of the element to get.
- defaultValue: AnyValueArray - default value
- returns: AnyValueArray - AnyValueArray value of the element or given default value if the conversion is not supported.
GetAsBoolean
Converts a map element into a bool or returns false if the conversion is not possible.
public
bool GetAsBoolean(string key)
- key: string - key of the element to get.
- returns: bool - bool value of the element or false if the conversion is not supported.
GetAsBooleanWithDefault
Converts a map element into a bool or returns a given default value if the conversion is not possible.
See BooleanConverter.ToBooleanWithDefault
public
bool GetAsBooleanWithDefault(string key, bool defaultValue)
- key: string - key of element to get.
- defaultValue: bool - default value
- returns: bool - bool value of the element or given default value if the conversion is not supported.
GetAsDateTime
Converts a map element into a DateTime or returns the current date if the conversion is not possible.
See BooleanConverter.toBooleanWithDefault
public
DateTime GetAsDateTime(string key)
- key: string - key of element to get.
- returns: DateTime - DateTime value of the element or the current date if the conversion is not supported.
GetAsDateTimeWithDefault
Converts a map element into a DateTime or returns a given default value if the conversion is not possible.
See DateTimeConverter.ToDateTimeWithDefault
public
DateTime GetAsDateTimeWithDefault(string key, DateTime defaultValue)
- key: string - key of element to get.
- defaultValue: DateTime - default value
- returns: DateTime - DateTime value of the element or given default value if the conversion is not supported.
GetAsNullableTimeSpan
Converts a map element into a TimeSpan or returns null if the conversion is not possible.
public
TimeSpan GetAsNullableTimeSpan(string key)
- key: string - key of the element to get
- returns: TimeSpan - TimeSpan of the element or null if the conversion is not possible.
GetAsTimeSpan
Converts a map element into a TimeSpan.
public
TimeSpan GetAsTimeSpan(string key)
- key: string - key of the element to get
- returns: TimeSpan - TimeSpan of the element
GetAsTimeSpanWithDefault
Converts a map element into a TimeSpan or returns a given default value if the conversion is not possible.
public
TimeSpan GetAsTimeSpanWithDefault(string key, TimeSpan? defaultValue)
- key: string - key of the element to get
- defaultValue: TimeSpan - default value
- returns: TimeSpan - Timespan of the element or given default value if the conversion is not possible.
GetAsNullableEnum
Converts a map element into an Enum or returns null if the conversion is not possible.
public
T GetAsNullableEnum<T>(string key)
- key: string - key of the element to get
- returns: T - Enum of the element or null if the conversion is not possible
GetAsEnum
Converts a map element into an Enum.
public
T GetAsEnum<T>(string key)
- key: string - key of the element to get
- returns: T - Enum of the element.
GetAsEnumWithDefault
Converts a map element into an Enum or returns a given default value if the conversion is not possible.
public
T GetAsEnumWithDefault<T>(string key, T defaultValue)
- key: string - key of the element to get
- defaultValue: T - default value
- returns: T - Enum or given default value if the conversion is not possible.
GetAsDouble
Converts a map element into a double or returns 0 if the conversion is not possible.
public
double GetAsDouble(string key)
- key: string - key of element to get.
- returns: double - double value of the element or 0 if the conversion is not supported.
GetAsDoubleWithDefault
Converts a map element into a double or returns a given default value if the conversion is not possible.
See DoubleConverter.ToDoubleWithDefault
public
double GetAsDoubleWithDefault(string key, double defaultValue)
- key: string - key of element to get.
- defaultValue: double - default value
- returns: double - double value of the element or given default value if the conversion is not supported.
GetAsFloat
Converts a map element into a float or returns 0 if the conversion is not possible.
public
float GetAsFloat(string key)
- key: string - key of element to get.
- returns: float - float value of the element or 0 if the conversion is not supported.
GetAsFloatWithDefault
Converts a map element into a float or returns a given default value if the conversion is not possible.
See FloatConverter.ToFloatWithDefault
public
float GetAsFloatWithDefault(string key, float defaultValue)
- key: string - key of the element to get.
- defaultValue: float - default value
- returns: float - float value of the element or given default value if the conversion is not supported.
GetAsInteger
Converts a map element into an integer or returns 0 if the conversion is not possible.
public
int GetAsInteger(string key)
- key: string - key of element to get.
- returns: int - integer value of the element or 0 if the conversion is not supported.
GetAsIntegerWithDefault
Converts a map element into an integer or returns a given default value if the conversion is not possible.
See IntegerConverter.ToIntegerWithDefault
public
int GetAsIntegerWithDefault(string key, int defaultValue)
- key: string - key of element to get.
- defaultValue: int - default value
- returns: int - integer value of the element or given default value if the conversion is not supported.
GetAsLong
Converts a map element into a long or returns 0 if the conversion is not possible.
public
long GetAsLong(long key)
- key: string - key of element to get.
- returns: long - long value of the element or 0 if the conversion is not supported.
GetAsLongWithDefault
Converts a map element into a long or returns a given default value if the conversion is not possible.
See LongConverter.ToLongWithDefault
public
long GetAsLongWithDefault(string key, long defaultValue)
- key: string - key of element to get.
- defaultValue: long - default value
- returns: long - long value of the element or given default value if the conversion is not supported.
GetAsMap
Converts a map element into an AnyValueMap or returns an empty AnyValueMap if the conversion is not possible.
public
AnyValueMap GetAsMap(string key)
- key: string - key of element to get.
- returns: AnyValueMap - long value of the element or an empty AnyValueMap if the conversion is not supported.
GetAsMapWithDefault
Converts a map element into an AnyValueMap or returns a given default value if the conversion is not possible.
public
AnyValueMap GetAsMapWithDefault(string key, AnyValueMap defaultValue)
- key: string - key of element to get.
- defaultValue: AnyValueMap - default value
- returns: AnyValueMap - AnyValueMap value of the element or give default value if the conversion is not supported.
GetAsNullableArray
Converts a map element into an AnyValueArray or returns null if the conversion is not possible.
See AnyValueMap, AnyValueMap.FromValue
public
AnyValueMap GetAsNullableArray(string key)
- key: string - key of the element to get.
- returns: AnyValueMap - AnyValueMap value of the element or null if the conversion is not supported.
GetAsNullableBoolean
Converts a map element into a bool or returns null if the conversion is not possible.
See BooleanConverter.ToNullableBoolean
public
bool GetAsNullableBoolean(string key)
- key: string - key of the element to get.
- returns: bool - bool value of the element or null if the conversion is not supported.
GetAsNullableDateTime
Converts a map element into a DateTime or returns null if the conversion is not possible.
See DateTimeConverter.ToNullableDateTime
public
DateTime GetAsNullableDateTime(string key)
- key: string - key of the element to get.
- returns: DateTime - DateTime value of the element or null if the conversion is not supported.
GetAsNullableDouble
Converts map element into a double or returns null if the conversion is not possible.
See DoubleConverter.ToNullableDouble
public
double GetAsNullableDouble(string key)
- key: string - key of the element to get.
- returns: double - double value of the element or null if the conversion is not supported.
GetAsNullableFloat
Converts a map element into a float or returns null if the conversion is not possible.
See FloatConverter.ToNullableFloat
public
float GetAsNullableFloat(string key)
- key: string - key of the element to get.
- returns: float - float value of the element or null if the conversion is not supported.
GetAsNullableInteger
Converts a map element into an integer or returns null if the conversion is not possible.
See IntegerConverter.ToNullableInteger
public
int GetAsNullableInteger(string key)
- key: string - key of the element to get.
- returns: int - integer value of the element or null if the conversion is not supported.
GetAsNullableLong
Converts a map element into a long or returns null if the conversion is not possible.
See LongConverter.ToNullableLong
public
long GetAsNullableLong(string key)
- key: string - key of the element to get.
- returns: long - long value of the element or null if the conversion is not supported.
GetAsNullableMap
Converts a map element into an AnyValueMap or returns null if the conversion is not possible.
public
AnyValueMap GetAsNullableMap(string key)
- key: string - key of the element to get.
- returns: AnyValueMap - AnyValueMap value of the element or null if the conversion is not supported.
GetAsNullableString
Converts a map element into a string or returns null if the conversion is not possible.
See StringConverter.ToNullableString
public
string GetAsNullableString(string key)
- key: string - key of the element to get.
- returns: string - string value of the element or null if the conversion is not supported.
GetAsNullableType
Converts a map element into a value defined by a specified typecode.
If conversion is not possible, it returns null.
T - the class type.
See TypeConverter.ToNullableType
public
T GetAsNullableType<T>(string key)
- key: string - key of the element to get.
- returns: T - element value defined by the typecode or null if the conversion is not supported.
GetAsObject
Gets the value stored in a map element without any conversions. When element key is not defined, it returns the entire map.
public
object GetAsObject(string key)
- key: string - (optional) key of the element to get
- returns: object - element value or entire map when the key is not defined.
GetAsObject
Gets the value stored in this map element without any conversions
public
object GetAsObject()
- returns: object - element value or value of the map when key is not defined.
GetAsString
Converts a map element into a string or returns "" if the conversion is not possible.
public
string GetAsString(string key)
- key: string - (optional) key of the element to get
- returns: string - string value of the element or "" if the conversion is not supported.
GetAsStringWithDefault
Converts a map element into a string or returns "" if the conversion is not possible.
public
string GetAsStringWithDefault(string key, string defaultValue)
- key: string - key of element to get.
- defaultValue: string - default value
- returns: string - string value of the element or default value if the conversion is not supported.
GetAsType
Converts a map element into a value defined by a specified typecode. If conversion is not possible, it returns the default value for the specified type. T - class type.
public
string GetAsType<T>(string key)
- key: string - key of element to get.
- returns: T - element value defined by the typecode or default if the conversion is not supported.
GetAsTypeWithDefault
Converts a map element into a value defined by a specified typecode.
If conversion is not possible, it returns the default value for the specified type.
T - class type.
See TypeConverter.ToTypeWithDefault
public
T GetAsTypeWithDefault<T>(string key, T defaultValue)
- key: string - key of the element to get.
- defaultValue: T - default value
- returns: T - element value defined by the typecode or default value if the conversion is not supported.
GetAsValue
Converts a map element into an AnyValue or returns an empty AnyValue if the conversion is not possible.
See AnyValue, AnyValue.constructors
public
AnyValue GetAsValue(string key)
- key: string - key of the element to get.
- returns: AnyValue - AnyValue value of the element or empty AnyValue if the conversion is not supported.
Set
Sets a new value into a map element specified by its key.
public virtual
void Set(string key, string value)
- key: string - key of the element to put.
- value: string - new value for the map element.
SetAsObject
Sets a new value to a map element specified by its index. When the index is not defined, it resets the entire map value. See MapConverter.ToMap
public
void SetAsObject(string key, object value)
- key: string - (optional) key of the element to set
- value: object - new element or map value.
SetAsObject
Sets a new value for this array element
public
void SetAsObject(object value)
- value: object - new element or map value.
ToString
Gets a string representation of the object. The result is a semicolon-separated list of key-value pairs as “key1=value1;key2=value2;key=value3”
public override
string ToString()
- returns: string - string representation of the object.
Static methods
FromMaps
Creates a new AnyValueMap by merging two or more maps. Maps defined later in the list override values from previously defined maps.
public static
StringValueMap FromMaps(params IDictionary<string, string>[] maps)
- maps: IDictionary<string, string>[] - array of maps to be merged
- returns: StringValueMap - newly created StringValueMap.
FromString
Parses semicolon-separated key-value pairs and returns them as a StringValueMap.
public static
StringValueMap FromString(string line)
- line: string - semicolon-separated key-value list to initialize StringValueMap.
- returns: StringValueMap - newly created StringValueMap.
FromTuples
Creates a new StringValueMap from a list of key-value pairs called tuples.
public static
StringValueMap FromTuples(object[] tuples)
- tuples: object[] - list of values where odd elements are keys and the following even elements are values
- returns: StringValueMap - newly created StringValueMap.
FromTuplesArray
Creates a new StringValueMap from a list of key-value pairs called tuples. The method is similar to FromTuples but tuples are passed as an array instead of as parameters.
public static
StringValueMap FromTuplesArray(params object[] tuples)
- tuples: object[] - list of values where odd elements are keys and the following even elements are values
- returns: StringValueMap - newly created StringValueMap.
FromValue
Converts a specified value into StringValueMap.
public static
StringValueMap FromValue(object value)
- value: object - value to be converted
- returns: StringValueMap - newly created StringValueMap.
Examples
var value1 = StringValueMap.FromString("key1=1;key2=123.456;key3=2018-01-01");
value1.GetAsBoolean("key1"); // Result: true
value1.GetAsInteger("key2"); // Result: 123
value1.GetAsFloat("key2"); // Result: 123.456
value1.GetAsDateTime("key3"); // Result: new DateTime(2018,0,1)
See also
-
StringConverter
-
TypeConverter
-
StringConverter
-
BooleanConverter
-
IntegerConverter
-
LongConverter
-
DoubleConverter
-
FloatConverter
-
DateTimeConverter
-
ICloneable