DictionaryExtensions
Dictionary extension methods
public static class DictionaryExtensions
Namespace
Methods
GetTypedValueAs<T>
Get the value of dictionary cast to a given Type
public static T GetTypedValueAs<T>(this IDictionary<string, object> dict, string key)
Parameters
Parameter | Description |
---|---|
T |
The Type to cast the Value to |
dict |
The dictionary to retrieve the Value from |
key |
The Key of the Value to retrieve |
Returns
The cast dictionary Value, or default(T) if the Value is not present or cannot be cast to the given type
ToNullSafeDictionary<TKey,TValue> (1 of 2)
Converts a dictionary into a null safe dictionary that won't error if attempting to access a key that isn't present
public static NullSafeDictionary<TKey, TValue> ToNullSafeDictionary<TKey, TValue>(
this IDictionary<TKey, TValue> source)
where TValue : class
Parameters
Parameter | Description |
---|---|
TKey |
The Type of the dictionary key |
TValue |
The Type of the dictionary value |
source |
The source dictionary to convert |
Returns
A null safe version of the input dictionary
ToNullSafeDictionary<TKey,TValue> (2 of 2)
Converts a dictionary into a null safe dictionary that won't error if attempting to access a key that isn't present
public static NullSafeDictionary<TKey, TValue> ToNullSafeDictionary<TKey, TValue>(
this IDictionary<TKey, TValue> source, IEqualityComparer<TKey> keyComparer)
where TValue : class
Parameters
Parameter | Description |
---|---|
TKey |
The Type of the dictionary key |
TValue |
The Type of the dictionary value |
source |
The source dictionary to convert |
keyComparer |
The IEqualityComparer implementation to use when comparing keys |
Returns
A null safe version of the input dictionary
ToNullSafeDictionary<TSource,TKey,TValue> (1 of 2)
Converts a list of items into a null safe dictionary that won't error if attempting to access a key that isn't present
public static NullSafeDictionary<TKey, TValue> ToNullSafeDictionary<TSource, TKey, TValue>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
Func<TSource, TValue> elementSelector)
where TValue : class
Parameters
Parameter | Description |
---|---|
TSource |
The Type of the source element |
TKey |
The Type of the dictionary key |
TValue |
The Type of the dictionary value |
source |
The source list of elements to convert |
keySelector |
A delegate function to access the Key from the source element |
elementSelector |
A delegate function to access the Value from the source element |
Returns
A null safe version of the input dictionary
ToNullSafeDictionary<TSource,TKey,TValue> (2 of 2)
Converts a list of items into a null safe dictionary that won't error if attempting to access a key that isn't present
public static NullSafeDictionary<TKey, TValue> ToNullSafeDictionary<TSource, TKey, TValue>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
Func<TSource, TValue> elementSelector, IEqualityComparer<TKey> keyComparer)
where TValue : class
Parameters
Parameter | Description |
---|---|
TSource |
The Type of the source element |
TKey |
The Type of the dictionary key |
TValue |
The Type of the dictionary value |
source |
The source list of elements to convert |
keySelector |
A delegate function to access the Key from the source element |
elementSelector |
A delegate function to access the Value from the source element |
keyComparer |
The IEqualityComparer implementation to use when comparing keys |
Returns
A null safe version of the input dictionary
ToPropertyCollection<TValue>
public static PropertyCollection<TValue> ToPropertyCollection<TValue>(
this IDictionary<string, TValue> source)
where TValue : class
ToPropertyCollection<TSource,TValue>
public static PropertyCollection<TValue> ToPropertyCollection<TSource, TValue>(
this IEnumerable<TSource> source, Func<TSource, string> keySelector,
Func<TSource, TValue> elementSelector)
where TValue : class
TryGetTypedValue<T>
Try to get the value of dictionary cast to a given Type
public static bool TryGetTypedValue<T>(this IDictionary<string, object> dict, string key,
out T result)
Parameters
Parameter | Description |
---|---|
T |
The Type to cast the Value to |
dict |
The dictionary to retrieve the Value from |
key |
The Key of the Value to retrieve |
result |
The cast dictionary Value, or default(T) if the Value is not present or cannot be cast to the given type |
Returns
Boolean indicating whether the operation was successful or not