DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="0.1.0" />

 Dictionary

public static class Dictionary
Represents various extensions for types Dictionary<T, U> and IDictionary<T, U>.
using System; using System.Collections.Generic; using System.Reflection; namespace DotNext.Collections.Generic { public static class Dictionary { private static class Indexer<D, K, V> where D : class, IEnumerable<KeyValuePair<K, V>> { internal static readonly Func<D, K, V> Getter; internal static readonly Action<D, K, V> Setter; static Indexer() { MemberInfo[] defaultMembers = typeof(D).GetDefaultMembers(); for (int i = 0; i < defaultMembers.Length; i++) { PropertyInfo propertyInfo; if ((object)(propertyInfo = (defaultMembers[i] as PropertyInfo)) != null) { Getter = DelegateHelpers.CreateDelegate<Func<D, K, V>>(propertyInfo.GetMethod, (object)null); MethodInfo setMethod = propertyInfo.SetMethod; Setter = (((object)setMethod != null) ? DelegateHelpers.CreateDelegate<Action<D, K, V>>(setMethod, (object)null) : null); return; } } throw new MissingMemberException(); } } public static class Indexer<K, V> { public static Func<IReadOnlyDictionary<K, V>, K, V> ReadOnly => Indexer<IReadOnlyDictionary<K, V>, K, V>.Getter; public static Func<IDictionary<K, V>, K, V> Getter => Indexer<IDictionary<K, V>, K, V>.Getter; public static Action<IDictionary<K, V>, K, V> Setter => Indexer<IDictionary<K, V>, K, V>.Setter; } public static Func<K, V> IndexerGetter<K, V>(this IReadOnlyDictionary<K, V> dictionary) { return DelegateHelpers.CreateDelegate<Func<K, V>>(Indexer<K, V>.ReadOnly.Method, dictionary); } public static Func<K, V> IndexerGetter<K, V>(this IDictionary<K, V> dictionary) { return DelegateHelpers.CreateDelegate<Func<K, V>>(Indexer<K, V>.Getter.Method, dictionary); } public static Action<K, V> IndexerSetter<K, V>(this IDictionary<K, V> dictionary) { return DelegateHelpers.CreateDelegate<Action<K, V>>(Indexer<K, V>.Setter.Method, dictionary); } public static void Deconstruct<K, V>(this KeyValuePair<K, V> pair, out K key, out V value) { key = pair.Key; value = pair.Value; } public static V GetOrAdd<K, V>(this Dictionary<K, V> dictionary, K key, V value) { if (dictionary.TryGetValue(key, out V value2)) value = value2; else dictionary.Add(key, value); return value; } public static V GetOrAdd<K, V>(this Dictionary<K, V> dictionary, K key, Func<K, V> valueFactory) { if (dictionary.TryGetValue(key, out V value)) return value; value = valueFactory(key); dictionary.Add(key, value); return value; } public static void ForEach<K, V>(this IDictionary<K, V> dictionary, Action<K, V> action) { foreach (KeyValuePair<K, V> item in (IEnumerable<KeyValuePair<K, V>>)dictionary) { Deconstruct(item, out K key, out V value); K arg = key; V arg2 = value; action(arg, arg2); } } public static V GetOrInvoke<K, V>(this IDictionary<K, V> dictionary, K key, Func<V> defaultValue) { if (!dictionary.TryGetValue(key, out V value)) return defaultValue(); return value; } public static Optional<T> ConvertValue<K, V, T>(this IDictionary<K, V> dictionary, K key, Converter<V, T> mapper) { if (!dictionary.TryGetValue(key, out V value)) return Optional<T>.Empty; return mapper(value); } public static bool ConvertValue<K, V, T>(this IDictionary<K, V> dictionary, K key, Converter<V, T> mapper, out T value) { return dictionary.ConvertValue(key, mapper).TryGet(out value); } public static ReadOnlyDictionaryView<K, V> AsReadOnlyView<K, V>(this IDictionary<K, V> dictionary) { return new ReadOnlyDictionaryView<K, V>(dictionary); } public static ReadOnlyDictionaryView<K, V, T> Convert<K, V, T>(this IReadOnlyDictionary<K, V> dictionary, Converter<V, T> mapper) { return new ReadOnlyDictionaryView<K, V, T>(dictionary, mapper); } } }