| | 1 | | using System.Collections.ObjectModel; |
| | 2 | |
|
| | 3 | | namespace Fluorite.Extensions; |
| | 4 | |
|
| | 5 | | public static class DictionaryExtensions |
| | 6 | | { |
| | 7 | | public static Dictionary<TKey, TValue> MergeLeft<TKey, TValue>( |
| | 8 | | this IReadOnlyDictionary<TKey, TValue> source, |
| | 9 | | params IReadOnlyDictionary<TKey, TValue>[] others) |
| | 10 | | { |
| 1 | 11 | | Guard.Against.Null(source); |
| 1 | 12 | | Guard.Against.Null(others); |
| | 13 | |
|
| 1 | 14 | | var resultDictionary = new Dictionary<TKey, TValue>(); |
| | 15 | |
|
| 6 | 16 | | foreach (var dictionary in new[] { source }.Concat(others)) |
| | 17 | | { |
| 8 | 18 | | foreach (var pair in dictionary) |
| | 19 | | { |
| 2 | 20 | | resultDictionary[pair.Key] = pair.Value; |
| | 21 | | } |
| | 22 | | } |
| | 23 | |
|
| 1 | 24 | | return resultDictionary; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public static ReadOnlyDictionary<TKey, TValue> ToReadOnly<TKey, TValue>( |
| | 28 | | this IDictionary<TKey, TValue> source) |
| | 29 | | { |
| 35 | 30 | | Guard.Against.Null(source); |
| | 31 | |
|
| 35 | 32 | | return new ReadOnlyDictionary<TKey, TValue>(source); |
| | 33 | | } |
| | 34 | | } |