Inherits: ICloneable, Dictionary<string, object>
Description
The AnyValueMap class provides a cross-language implementation of a dynamic map (dictionary) object that can hold values of any type. In addition, it provides several methods to convert the stored values to different types, such as bool, integer or datetime.
Constructors
Creates a new instance of the map and assigns its value.
publicAnyValueMap(IDictionary<string, object> values)
- values: IDictionary<string, object> - (optional) values to initialize this map.
Creates a new instance of the map and assigns its value.
publicAnyValueMap(IDictionary values)
- values: IDictionary - (optional) values to initialize this map.
Creates a new instance of the map and assigns its value.
publicAnyValueMap()
Instance methods
Append
Appends new elements to this map.
publicvoid Append(IDictionary map)
- map: IDictionary - map with elements to be added.
Append
Appends new elements to this map.
publicvoid Append(IDictionary<string, object> map)
- map: IDictionary<string, object> - map with elements to be added.
Clone
Creates a binary clone of this object.
publicobject Clone()
- returns: object - clone of this object.
Get
Gets a map element specified by its key.
public virtualobject Get(string key)
- key: string - key of the element to get.
- returns: object - 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
publicAnyValueArray GetAsArray(string key)
- key: string - key of the element to get.
- returns: AnyValueArray - AnyValueArray value of the element or 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
publicAnyValueArray 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.
publicbool GetAsBoolean(string key)
- key: string - key of the element to get.
- returns: 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
publicbool GetAsBooleanWithDefault(string key, bool defaultValue)
- key: string - key of the 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.
publicDateTime GetAsDateTime(string key)
- key: string - key of the 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
publicDateTime GetAsDateTimeWithDefault(string key, DateTime defaultValue)
- key: string - key of the 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.
publicTimeSpan GetAsNullableTimeSpan(string key)
- key: string - key of the element to get.
- returns: TimeSpan - TimeSpan or null if the conversion is not possible.
GetAsTimeSpan
Converts a map element into a TimeSpan.
publicTimeSpan GetAsTimeSpan(string key)
- key: string - key of the element to get.
- returns: TimeSpan - TimeSpan
GetAsTimeSpanWithDefault
Converts a map element into a TimeSpan or returns a given default value if the conversion is not possible.
publicTimeSpan GetAsTimeSpanWithDefault(string key, TimeSpan? defaultValue)
- key: string - key of the element to get.
- defaultValue: TimeSpan - default value
- returns: TimeSpan - TimeSpan
GetAsNullableEnum
Converts a map element into an Enum or returns null if the conversion is not possible.
publicT GetAsNullableEnum<T>(string key)
- key: string - key of the element to get.
- returns: T - Enum or null if the conversion is not possible.
GetAsEnum
Converts a map element into an Enum.
publicT GetAsEnum<T>(string key)
- key: string - key of the element to get.
- returns: T - Enum
GetAsEnumWithDefault
Converts a map element into an Enum or returns a given default value if the conversion is not possible.
publicT 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.
publicdouble GetAsDouble(string key)
- key: string - key of the 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
publicdouble GetAsDoubleWithDefault(string key, double defaultValue)
- key: string - key of the 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.
publicfloat GetAsFloat(string key)
- key: string - key of the 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
publicfloat 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.
publicint GetAsInteger(string key)
- key: string - key of the 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
publicint GetAsIntegerWithDefault(string key, float defaultValue)
- key: string - key of the element to get.
- defaultValue: float - default value
- returns: float - 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.
See IntegerConverter.ToIntegerWithDefault
publiclong GetAsLong(string key)
- key: string - key of the 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
publiclong GetAsLongWithDefault(string key, long defaultValue)
- key: string - key of the 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.
See LongConverter.ToLongWithDefault
publicAnyValueMap GetAsMap(string key)
- key: string - key of the element to get.
- returns: AnyValueMap - AnyValueMap value of the element or 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.
publicAnyValueMap GetAsMapWithDefault(string key, AnyValueMap defaultValue)
- key: string - key of the element to get.
- defaultValue: AnyValueMap - default value
- returns: AnyValueMap - AnyValueMap value of the element or given 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
publicAnyValueMap GetAsNullableArray(string key)
- key: string - key of the element to get.
- returns: AnyValueMap - AnyValueArray 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
publicbool 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 map element into a long or returns a given default value if the conversion is not possible.
See DateTimeConverter.ToNullableDateTime
publicDateTime 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 a map element into a double or returns null if the conversion is not possible.
See DoubleConverter.ToNullableDouble
publicdouble 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
publicfloat 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
publicint GetAsNullableInteger(string key)
- key: string - key of 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
publiclogn GetAsNullableLong(string key)
- key: string - key of element to get.
- returns: logn - 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.
publicAnyValueMap GetAsNullableMap(string key)
- key: string - key of 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
publicstring 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 the conversion is not possible, it returns null.
See TypeConverter.toNullableType
T - class type
publicT 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 this map element without any conversions.
publicobject GetAsObject()
- key: string - (optional) key of the element to get
- returns: object - value of the map element.
GetAsObject
Gets the value stored in a map element without any conversions. When element key is not defined, it returns the entire map value.
publicobject GetAsObject(string key)
- key: string - (optional) key of the element to get
- returns: object - element value or value of the map when the index is not defined.
GetAsString
Converts a map element into a string or returns "" if conversion is not possible.
publicstring GetAsString(string key)
- key: string - 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 a given default value if the conversion is not possible. See StringConverter.ToStringWithDefault
publicstring GetAsStringWithDefault(string key, string defaultValue)
- key: string - key of the 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 map element into a value defined by a specified typecode. If the conversion is not possible, it returns the default value for the specified type. T - class type
publicT GetAsType<T>(string key)
- key: string - key of the 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 the conversion is not possible, it returns a given default value.
T - class type
See TypeConverter.ToTypeWithDefault
publicT GetAsTypeWithDefault<T>(string key, T defaultValue)
- key: string - key of the element to get.
- defaultValue: T - the default value
- returns: T - element value defined by the typecode or default given 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
publicAnyValue GetAsValue(string key)
- key: string - key of element to get.
- returns: AnyValue -AnyValue value of the element or empty AnyValue if the conversion is not supported.
Set
Puts a new value into a map element specified by its key.
public virtualSet(string key, object value)
- key: string - key of the element to put.
- value: object - new value for the map element.
SetAsObject
Sets a new value for this array element
publicvoid SetAsObject(object value)
- value: object - new element or map value.
SetAsObject
Sets a new value to a map element specified by its index. When the index is not defined, it resets the entire map.
publicvoid SetAsObject(string key, object value)
- key: string - (optional) key of the element to set
- value: object - new element or map value.
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 staticAnyValueMap FromMaps(params IDictionary[] maps)
- maps: IDictionary[] - array of maps to be merged
- returns: AnyValueMap - newly created AnyValueMap.
FromTuples
Creates a new AnyValueMap from a list of key-value pairs called tuples.
public staticAnyValueMap FromTuples(params object[] tuples)
- tuples: object[] - list of values where odd elements are keys and the following even elements are values.
- returns: AnyValueMap - newly created AnyValueArray.
FromTuplesArray
Creates a new AnyValueMap from a list of key-value pairs called tuples. The method is similar to fromTuples but tuples are passed as an array instead of parameters.
public staticAnyValueMap FromTuplesArray(params object[] tuples)
- tuples: object[] - list of values where odd elements are keys and the following even elements are values.
- returns: AnyValueMap - newly created AnyValueArray.
FromValue
Converts a specified value into an AnyValueMap.
public staticAnyValueMap FromValue(object value)
- value: object - value to be converted
- returns: AnyValueMap - newly created AnyValueMap.
Examples
var value1 = new AnyValueMap(new Dictionary<string, object>{
{"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