| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="IDictionary{TKey, TValue}"/>. |
| | 5 | | /// </summary> |
| | 6 | | // ReSharper disable once InconsistentNaming |
| | 7 | | public static class IDictionaryExtensions |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Converts the <see cref="IDictionary{TKey, TValue}"/> |
| | 11 | | /// to the <see cref="NameValueCollection"/>. |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="TKey">The type of the key.</typeparam> |
| | 14 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 15 | | /// <param name="dictionary">The set.</param> |
| | 16 | | /// <remarks> |
| | 17 | | /// For detail, see https://stackoverflow.com/a/7230446/22944 |
| | 18 | | /// </remarks> |
| | 19 | | public static NameValueCollection ToNameValueCollection<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) |
| 1 | 20 | | { |
| 1 | 21 | | var nameValueCollection = new NameValueCollection(); |
| | 22 | |
|
| 11 | 23 | | foreach (var kvp in dictionary) |
| 4 | 24 | | { |
| 4 | 25 | | string? value = null; |
| 4 | 26 | | if (kvp.Value != null) |
| 4 | 27 | | value = kvp.Value.ToString(); |
| | 28 | |
|
| 4 | 29 | | nameValueCollection.Add(kvp.Key!.ToString(), value); |
| 4 | 30 | | } |
| | 31 | |
|
| 1 | 32 | | return nameValueCollection; |
| 1 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Tries to get value with the specified key. |
| | 37 | | /// </summary> |
| | 38 | | /// <typeparam name="TKey">The type of the key.</typeparam> |
| | 39 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 40 | | /// <param name="dictionary">The dictionary.</param> |
| | 41 | | /// <param name="key">The key.</param> |
| | 42 | | public static TValue? TryGetValueWithKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) => |
| 2 | 43 | | dictionary.TryGetValueWithKey(key, throwException: false); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Tries to get value with the specified key. |
| | 47 | | /// </summary> |
| | 48 | | /// <typeparam name="TKey">The type of the key.</typeparam> |
| | 49 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 50 | | /// <param name="dictionary">The dictionary.</param> |
| | 51 | | /// <param name="key">The key.</param> |
| | 52 | | /// <param name="throwException">if set to <c>true</c> [throw exception].</param> |
| | 53 | | public static TValue? TryGetValueWithKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey? key, |
| | 54 | | bool throwException) |
| 3 | 55 | | { |
| 3 | 56 | | ArgumentNullException.ThrowIfNull(dictionary); |
| 3 | 57 | | ArgumentNullException.ThrowIfNull(key); |
| | 58 | |
|
| 3 | 59 | | var test = dictionary.TryGetValue(key, out var value); |
| | 60 | |
|
| 3 | 61 | | return value switch |
| 3 | 62 | | { |
| 3 | 63 | | null when !test && throwException => throw new NullReferenceException( |
| 1 | 64 | | $"The expected value from key, {key}, is not here."), |
| 2 | 65 | | null when !test => default, |
| 1 | 66 | | _ => value |
| 3 | 67 | | }; |
| 2 | 68 | | } |
| | 69 | | } |