1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Microsoft Public License. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Microsoft Public License, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Microsoft Public License.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using Microsoft.Scripting.Ast;
19 using System.Linq.Expressions;
25 using System.Collections.Generic;
27 namespace System.Dynamic.Utils {
29 // Like ReadOnlyCollection<T>: wraps an IDictionary<K, V> in a read-only wrapper
30 internal sealed class ReadOnlyDictionary<K, V> : IDictionary<K, V> {
32 // For wrapping non-readonly Keys, Values collections
33 // Not used for standard dictionaries, which return read-only Keys and Values
34 private sealed class ReadOnlyWrapper<T> : ICollection<T> {
35 // no idea why this warning is here
36 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
37 private readonly ICollection<T> _collection;
39 internal ReadOnlyWrapper(ICollection<T> collection) {
40 _collection = collection;
43 #region ICollection<T> Members
45 public void Add(T item) {
46 throw Error.CollectionReadOnly();
50 throw Error.CollectionReadOnly();
53 public bool Contains(T item) {
54 return _collection.Contains(item);
57 public void CopyTo(T[] array, int arrayIndex) {
58 _collection.CopyTo(array, arrayIndex);
62 get { return _collection.Count; }
65 public bool IsReadOnly {
69 public bool Remove(T item) {
70 throw Error.CollectionReadOnly();
75 #region IEnumerable<T> Members
77 public IEnumerator<T> GetEnumerator() {
78 return _collection.GetEnumerator();
83 #region IEnumerable Members
85 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
86 return _collection.GetEnumerator();
92 private readonly IDictionary<K, V> _dict;
94 internal ReadOnlyDictionary(IDictionary<K, V> dict) {
95 ReadOnlyDictionary<K, V> rodict = dict as ReadOnlyDictionary<K, V>;
96 _dict = (rodict != null) ? rodict._dict : dict;
99 #region IDictionary<K,V> Members
101 public bool ContainsKey(K key) {
102 return _dict.ContainsKey(key);
105 public ICollection<K> Keys {
107 ICollection<K> keys = _dict.Keys;
108 if (!keys.IsReadOnly) {
109 return new ReadOnlyWrapper<K>(keys);
115 public bool TryGetValue(K key, out V value) {
116 return _dict.TryGetValue(key, out value);
119 public ICollection<V> Values {
121 ICollection<V> values = _dict.Values;
122 if (!values.IsReadOnly) {
123 return new ReadOnlyWrapper<V>(values);
129 public V this[K key] {
136 void IDictionary<K, V>.Add(K key, V value) {
137 throw Error.CollectionReadOnly();
140 bool IDictionary<K, V>.Remove(K key) {
141 throw Error.CollectionReadOnly();
144 V IDictionary<K, V>.this[K key] {
149 throw Error.CollectionReadOnly();
155 #region ICollection<KeyValuePair<K,V>> Members
157 public bool Contains(KeyValuePair<K, V> item) {
158 return _dict.Contains(item);
161 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) {
162 _dict.CopyTo(array, arrayIndex);
166 get { return _dict.Count; }
169 public bool IsReadOnly {
173 void ICollection<KeyValuePair<K, V>>.Add(KeyValuePair<K, V> item) {
174 throw Error.CollectionReadOnly();
177 void ICollection<KeyValuePair<K, V>>.Clear() {
178 throw Error.CollectionReadOnly();
181 bool ICollection<KeyValuePair<K,V>>.Remove(KeyValuePair<K, V> item) {
182 throw Error.CollectionReadOnly();
187 #region IEnumerable<KeyValuePair<K,V>> Members
189 public IEnumerator<KeyValuePair<K, V>> GetEnumerator() {
190 return _dict.GetEnumerator();
195 #region IEnumerable Members
197 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
198 return _dict.GetEnumerator();