-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathUnmodifiableDictionary.cs
130 lines (104 loc) · 3.05 KB
/
UnmodifiableDictionary.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections;
using System.Collections.Generic;
namespace NHibernate.Util
{
/// <summary>
/// Returns an unmodifiable view of the specified IDictionary.
/// This method allows modules to provide users with "read-only" access to internal dictionary.
/// Query operations on the returned dictionary "read through" to the specified dictionary,
/// and attempts to modify the returned dictionary,
/// whether direct or via its collection views, result in an <see cref="NotSupportedException"/>.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
//Since v5.1
[Obsolete("Please use System.Collections.ObjectModel.ReadOnlyDictionary<K,V> instead.")]
[Serializable]
public class UnmodifiableDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> dictionary;
/// <summary>
/// Initializes a new instance of the UnmodifiableDictionary class that contains elements wrapped
/// from the specified IDictionary.
/// </summary>
/// <param name="dictionary">The <see cref="IDictionary{TK,TV}"/> whose elements are wrapped.</param>
public UnmodifiableDictionary(IDictionary<TKey, TValue> dictionary)
{
this.dictionary = dictionary;
}
#region IDictionary<TKey,TValue> Members
public bool ContainsKey(TKey key)
{
return dictionary.ContainsKey(key);
}
public void Add(TKey key, TValue value)
{
throw new NotSupportedException();
}
public bool Remove(TKey key)
{
throw new NotSupportedException();
}
public bool TryGetValue(TKey key, out TValue value)
{
return dictionary.TryGetValue(key, out value);
}
public TValue this[TKey key]
{
get { return dictionary[key]; }
set { throw new NotSupportedException(); }
}
public ICollection<TKey> Keys
{
get { return dictionary.Keys; }
}
public ICollection<TValue> Values
{
get { return dictionary.Values; }
}
#endregion
#region ICollection<KeyValuePair<TKey,TValue>> Members
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return dictionary.Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
dictionary.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new NotSupportedException();
}
public int Count
{
get { return dictionary.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return dictionary.GetEnumerator();
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return ((IEnumerable<KeyValuePair<TKey, TValue>>) this).GetEnumerator();
}
#endregion
}
}