-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCastedStringKeyDictionary.cs
More file actions
73 lines (54 loc) · 2.22 KB
/
CastedStringKeyDictionary.cs
File metadata and controls
73 lines (54 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace CommonNet.Extensions.Tests;
public class CastedStringKeyDictionary(IDictionary sourceDictionary) : IDictionary<string, object?>
{
private readonly IDictionary _nestedDictionary = sourceDictionary;
public ICollection<string> Keys => _nestedDictionary.Keys.Cast<string>().ToList();
public ICollection<object?> Values => _nestedDictionary.Values.Cast<object?>().ToList();
public int Count => _nestedDictionary.Count;
public bool IsReadOnly => _nestedDictionary.IsReadOnly;
public object? this[string key]
{
get => _nestedDictionary[key];
set => _nestedDictionary[key] = value;
}
public void Add(string key, object? value) => _nestedDictionary.Add(key, value);
public bool ContainsKey(string key) => _nestedDictionary.Contains(key);
public bool Remove(string key)
{
if (_nestedDictionary.Contains(key))
{
_nestedDictionary.Remove(key);
return true;
}
return false;
}
public bool TryGetValue(string key, [MaybeNullWhen(false)] out object? value)
{
if (_nestedDictionary.Contains(key))
{
value = _nestedDictionary[key];
return true;
}
value = null;
return false;
}
public void Add(KeyValuePair<string, object?> item) => _nestedDictionary.Add(item.Key, item.Value);
public void Clear() => _nestedDictionary.Clear();
public bool Contains(KeyValuePair<string, object?> item) =>
_nestedDictionary.Contains(item.Key) && EqualityComparer<object?>.Default.Equals(this[item.Key], item.Value);
public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) => _nestedDictionary.CopyTo(array, arrayIndex);
public bool Remove(KeyValuePair<string, object?> item)
{
if (Contains(item))
{
_nestedDictionary.Remove(item.Key);
return true;
}
return false;
}
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() =>
_nestedDictionary.Cast<KeyValuePair<string, object?>>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _nestedDictionary.GetEnumerator();
}