PeanutButter
Loading...
Searching...
No Matches
PeanutButter.Utils.DictionaryExtensions Class Reference

Provides extensions to convert from non-generic IDictionary to a generic one. More...

Static Public Member Functions

static IDictionary< TKey, TValue > ToDictionary< TKey, TValue > (this IDictionary dict, Func< DictionaryEntry, TKey > keyGenerator, Func< DictionaryEntry, TValue > valueGenerator)
 Convert the given IDictionary to IDictionary<TKey, TValue> with the provided key and value generator functions.
static IDictionary< TKey, TValue > ToDictionary< TKey, TValue > (this IDictionary dict)
 Converts the given non-generic IDictionary to IDictionary<TKey, TValue> with hard casting of keys and values in the source to the provided types.
static IDictionary< TKey, TValue > Inverted< TKey, TValue > (this IDictionary< TValue, TKey > src)
 Produces a new dictionary where the key and value have been inverted, ie the values become keys. This will only work if the values are distinct.
static IDictionary< TKey, TValue > Inverted< TKey, TValue > (this IDictionary< TValue, TKey > src, IEqualityComparer< TKey > keyComparer)
 Produces a new dictionary where the key and value have been inverted, ie the values become keys. This will only work if the values are distinct.
static IDictionary< string, string > ToDictionary (this NameValueCollection nameValueCollection)
 Converts a NameValueCollection.
static IDictionary< string, string > ToDictionary< T1, T2 > (this IEnumerable< KeyValuePair< T1, T2 > > src)
 Provides a string/string dictionary for anything providing the IEnumerable<KeyValuePair<T1, T2>> interface, eg query / form collections.
static IDictionary< string, string > ToDictionary< T1, T2 > (this IEnumerable< KeyValuePair< T1, T2 > > src, Func< T1, string > keyConverter, Func< T2, string > valueConverter)
 Provides a string/string dictionary for anything providing the IEnumerable<KeyValuePair<T1, T2>> interface, providing the mechanism for the caller to convert to strings.
static IDictionary< string, string > ToDictionary (this IDictionary dict)
 Shorthand for .ToDictionary<string, string>()
static TValue FindOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dict, TKey key, TValue value)
 Find an item in or add an item to a dictionary.
static TValue FindOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dict, TKey key, Func< TValue > generator)
 Find an item in or add an item to a dictionary.
static TValue FindOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dict, TKey key, Func< TValue > generator, Func< TValue, bool > skipCaching)
 Find an item in or add an item to a dictionary.
static TValue FindOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dict, TKey key, Func< TValue > generator, bool alwaysLock)
 Find an item in or add an item to a dictionary.
static TValue FindOrAdd< TKey, TValue > (this IDictionary< TKey, TValue > dict, TKey key, Func< TValue > generator, Func< TValue, bool > skipCaching, bool alwaysLock)
 Find an item in or add an item to a dictionary.
static IDictionary< TKey, TValue > Clone< TKey, TValue > (this IDictionary< TKey, TValue > dict)
 Clones a given dictionary - new collection, same items.
static IDictionary< TKey, TValue > MergedWith< TKey, TValue > (this IDictionary< TKey, TValue > first, IDictionary< TKey, TValue > second)
 Merge second dictionary into the first, producing a new dictionary output, with the second's values taking precedence over the first's.
static IDictionary< TKey, TValue > MergedWith< TKey, TValue > (this IDictionary< TKey, TValue > first, IDictionary< TKey, TValue > second, MergeWithPrecedence withPrecedence)
 Merge second dictionary into the first, producing a new dictionary output, with the provided merge-precedence.
static IDictionary< TKey, TValue > MergeInto< TKey, TValue > (this IDictionary< TKey, TValue > newData, IDictionary< TKey, TValue > target)
 Merges the new data into the target, preferring to keep the original values in the target when also specified in the new data.
static IDictionary< TKey, TValue > MergeInto< TKey, TValue > (this IDictionary< TKey, TValue > newData, IDictionary< TKey, TValue > target, MergeIntoPrecedence mergePrecedence)
 Merges the new data into the target, with the specified merge preference.
static IDictionary< string, string > ToDictionary (this NameValueCollection collection, IEqualityComparer< string > keyComparer)
 Converts a NameValueCollection to a dictionary with the provided key comparer.
static NameValueCollection ToNameValueCollection (this IDictionary< string, string > dict)
 Converts a NameValueCollection to a dictionary.
static bool TryAdd< TKey, TValue > (this IDictionary< TKey, TValue > subject, TKey key, TValue value)
 Attempts to add an item to the dictionary. Returns true if the item was added. Will lock the dictionary, so is thread-safe against itself or anything else calling lock() on the subject.
static IDictionary< TKey, TValue > AddOrUpdate< TKey, TValue > (this IDictionary< TKey, TValue > dictionary, TKey key, Func< TValue > generator, Func< TValue, TValue > mutator)
 Either add a new value to a dictionary or update the existing one.

Detailed Description

Provides extensions to convert from non-generic IDictionary to a generic one.

Member Function Documentation

◆ AddOrUpdate< TKey, TValue >()

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.AddOrUpdate< TKey, TValue > ( this IDictionary< TKey, TValue > dictionary,
TKey key,
Func< TValue > generator,
Func< TValue, TValue > mutator )
static

Either add a new value to a dictionary or update the existing one.

Parameters
dictionary
key
generator
mutator
Template Parameters
TKey
TValue
Returns

◆ Clone< TKey, TValue >()

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.Clone< TKey, TValue > ( this IDictionary< TKey, TValue > dict)
static

Clones a given dictionary - new collection, same items.

Parameters
dict
Template Parameters
TKey
TValue
Returns

◆ FindOrAdd< TKey, TValue >() [1/5]

TValue PeanutButter.Utils.DictionaryExtensions.FindOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue > dict,
TKey key,
Func< TValue > generator )
static

Find an item in or add an item to a dictionary.

  • operation is thread-safe:
    • relying on internal thread safety of ConcurrentDictionary
      • if the generator should definitely never be called more than once, consider the overload with alwaysLock: true
    • other dictionaries are locked during search & add
Parameters
dict
key
generator
Template Parameters
TKey
TValue
Returns
Exceptions
ArgumentNullException

◆ FindOrAdd< TKey, TValue >() [2/5]

TValue PeanutButter.Utils.DictionaryExtensions.FindOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue > dict,
TKey key,
Func< TValue > generator,
bool alwaysLock )
static

Find an item in or add an item to a dictionary.

  • operation is thread-safe:
    • concurrent dictionaries are optionally locked during search & add (see alwaysLock)
    • other dictionaries are locked during search & add
Parameters
dict
key
generator
alwaysLockWhen true, always lock during operations, even on ConcurrentDictionaries. This prevents the generator potentially being called twice by concurrent request. If that doesn't matter and you're operating on a ConcurrentDictionary, leave as false. Has no effect on anything other than ConcurrentDictionary.
Template Parameters
TKey
TValue
Returns
Exceptions
ArgumentNullException

◆ FindOrAdd< TKey, TValue >() [3/5]

TValue PeanutButter.Utils.DictionaryExtensions.FindOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue > dict,
TKey key,
Func< TValue > generator,
Func< TValue, bool > skipCaching )
static

Find an item in or add an item to a dictionary.

  • operation is thread-safe:
    • concurrent dictionaries are optionally locked during search & add (see alwaysLock)
    • other dictionaries are locked during search & add
Parameters
dict
key
generator
skipCaching
Template Parameters
TKey
TValue
Returns
Exceptions
ArgumentNullException

◆ FindOrAdd< TKey, TValue >() [4/5]

TValue PeanutButter.Utils.DictionaryExtensions.FindOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue > dict,
TKey key,
Func< TValue > generator,
Func< TValue, bool > skipCaching,
bool alwaysLock )
static

Find an item in or add an item to a dictionary.

  • operation is thread-safe:
    • concurrent dictionaries are optionally locked during search & add (see alwaysLock)
    • other dictionaries are locked during search & add
Parameters
dict
key
generator
skipCaching
alwaysLockWhen true, always lock during operations, even on ConcurrentDictionaries. This prevents the generator potentially being called twice by concurrent request. If that doesn't matter and you're operating on a ConcurrentDictionary, leave as false. Has no effect on anything other than ConcurrentDictionary.
Template Parameters
TKey
TValue
Returns
Exceptions
ArgumentNullException

◆ FindOrAdd< TKey, TValue >() [5/5]

TValue PeanutButter.Utils.DictionaryExtensions.FindOrAdd< TKey, TValue > ( this IDictionary< TKey, TValue > dict,
TKey key,
TValue value )
static

Find an item in or add an item to a dictionary.

  • operation is thread-safe: dictionary is locked during search & add
Parameters
dict
key
value
Template Parameters
TKey
TValue
Returns
Exceptions
ArgumentNullException

◆ Inverted< TKey, TValue >() [1/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.Inverted< TKey, TValue > ( this IDictionary< TValue, TKey > src)
static

Produces a new dictionary where the key and value have been inverted, ie the values become keys. This will only work if the values are distinct.

Parameters
src
Template Parameters
TKey
TValue
Returns

◆ Inverted< TKey, TValue >() [2/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.Inverted< TKey, TValue > ( this IDictionary< TValue, TKey > src,
IEqualityComparer< TKey > keyComparer )
static

Produces a new dictionary where the key and value have been inverted, ie the values become keys. This will only work if the values are distinct.

Parameters
src
keyComparer
Template Parameters
TKey
TValue
Returns

◆ MergedWith< TKey, TValue >() [1/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.MergedWith< TKey, TValue > ( this IDictionary< TKey, TValue > first,
IDictionary< TKey, TValue > second )
static

Merge second dictionary into the first, producing a new dictionary output, with the second's values taking precedence over the first's.

Parameters
first
second
Template Parameters
TKey
TValue
Returns

◆ MergedWith< TKey, TValue >() [2/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.MergedWith< TKey, TValue > ( this IDictionary< TKey, TValue > first,
IDictionary< TKey, TValue > second,
MergeWithPrecedence withPrecedence )
static

Merge second dictionary into the first, producing a new dictionary output, with the provided merge-precedence.

Parameters
first
second
withPrecedence
Template Parameters
TKey
TValue
Returns

◆ MergeInto< TKey, TValue >() [1/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.MergeInto< TKey, TValue > ( this IDictionary< TKey, TValue > newData,
IDictionary< TKey, TValue > target )
static

Merges the new data into the target, preferring to keep the original values in the target when also specified in the new data.

Parameters
newData
target
Template Parameters
TKey
TValue

◆ MergeInto< TKey, TValue >() [2/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.MergeInto< TKey, TValue > ( this IDictionary< TKey, TValue > newData,
IDictionary< TKey, TValue > target,
MergeIntoPrecedence mergePrecedence )
static

Merges the new data into the target, with the specified merge preference.

Parameters
newData
target
mergePrecedence
Template Parameters
TKey
TValue

◆ ToDictionary() [1/3]

IDictionary< string, string > PeanutButter.Utils.DictionaryExtensions.ToDictionary ( this IDictionary dict)
static

Shorthand for .ToDictionary<string, string>()

Parameters
dict
Returns

◆ ToDictionary() [2/3]

IDictionary< string, string > PeanutButter.Utils.DictionaryExtensions.ToDictionary ( this NameValueCollection collection,
IEqualityComparer< string > keyComparer )
static

Converts a NameValueCollection to a dictionary with the provided key comparer.

Parameters
collection
keyComparer
Returns

◆ ToDictionary() [3/3]

IDictionary< string, string > PeanutButter.Utils.DictionaryExtensions.ToDictionary ( this NameValueCollection nameValueCollection)
static

Converts a NameValueCollection.

Parameters
nameValueCollection
Returns

◆ ToDictionary< T1, T2 >() [1/2]

IDictionary< string, string > PeanutButter.Utils.DictionaryExtensions.ToDictionary< T1, T2 > ( this IEnumerable< KeyValuePair< T1, T2 > > src)
static

Provides a string/string dictionary for anything providing the IEnumerable<KeyValuePair<T1, T2>> interface, eg query / form collections.

Parameters
src
Template Parameters
T1
T2
Returns

◆ ToDictionary< T1, T2 >() [2/2]

IDictionary< string, string > PeanutButter.Utils.DictionaryExtensions.ToDictionary< T1, T2 > ( this IEnumerable< KeyValuePair< T1, T2 > > src,
Func< T1, string > keyConverter,
Func< T2, string > valueConverter )
static

Provides a string/string dictionary for anything providing the IEnumerable<KeyValuePair<T1, T2>> interface, providing the mechanism for the caller to convert to strings.

Parameters
src
keyConverter
valueConverter
Template Parameters
T1
T2
Returns

◆ ToDictionary< TKey, TValue >() [1/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.ToDictionary< TKey, TValue > ( this IDictionary dict)
static

Converts the given non-generic IDictionary to IDictionary<TKey, TValue> with hard casting of keys and values in the source to the provided types.

Parameters
dict
Template Parameters
TKey
TValue
Returns

◆ ToDictionary< TKey, TValue >() [2/2]

IDictionary< TKey, TValue > PeanutButter.Utils.DictionaryExtensions.ToDictionary< TKey, TValue > ( this IDictionary dict,
Func< DictionaryEntry, TKey > keyGenerator,
Func< DictionaryEntry, TValue > valueGenerator )
static

Convert the given IDictionary to IDictionary<TKey, TValue> with the provided key and value generator functions.

Parameters
dict
keyGenerator
valueGenerator
Template Parameters
TKey
TValue
Returns

◆ ToNameValueCollection()

NameValueCollection PeanutButter.Utils.DictionaryExtensions.ToNameValueCollection ( this IDictionary< string, string > dict)
static

Converts a NameValueCollection to a dictionary.

Parameters
dict
Returns

◆ TryAdd< TKey, TValue >()

bool PeanutButter.Utils.DictionaryExtensions.TryAdd< TKey, TValue > ( this IDictionary< TKey, TValue > subject,
TKey key,
TValue value )
static

Attempts to add an item to the dictionary. Returns true if the item was added. Will lock the dictionary, so is thread-safe against itself or anything else calling lock() on the subject.

Parameters
subject
key
value
Template Parameters
TKey
TValue
Returns

The documentation for this class was generated from the following file:
  • source/Utils/PeanutButter.Utils/DictionaryExtensions.cs