#if NET20 || NET30 || NET35 || NET40 || !NET_4_6 using System.Collections.Generic; using LinqInternal.Collections.Specialized; using LinqInternal.Core; namespace System.Collections.ObjectModel { [Serializable] public partial class ReadOnlyDictionary : IDictionary, IDictionary, IReadOnlyDictionary { private readonly KeyCollection _keys; private readonly ValueCollection _values; private readonly IDictionary _wrapped; public ReadOnlyDictionary(IDictionary dictionary) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } _wrapped = dictionary; _keys = new KeyCollection(new DelegatedCollection(() => _wrapped.Keys)); _values = new ValueCollection(new DelegatedCollection(() => _wrapped.Values)); } public int Count { get { return _wrapped.Count; } } public IDictionary Dictionary { get { return _wrapped; } } bool ICollection.IsSynchronized { get { return ((ICollection)_wrapped).IsSynchronized; } } object ICollection.SyncRoot { get { return ((ICollection)_wrapped).SyncRoot; } } bool ICollection>.IsReadOnly { get { return true; } } bool IDictionary.IsFixedSize { get { return ((IDictionary)_wrapped).IsFixedSize; } } bool IDictionary.IsReadOnly { get { return true; } } ICollection IDictionary.Keys { get { return _keys; } } ICollection IDictionary.Values { get { return _values; } } ICollection IDictionary.Keys { get { return _keys; } } ICollection IDictionary.Values { get { return _values; } } IEnumerable IReadOnlyDictionary.Keys { get { return _keys; } } IEnumerable IReadOnlyDictionary.Values { get { return _values; } } public KeyCollection Keys { get { return _keys; } } public ValueCollection Values { get { return _values; } } object IDictionary.this[object key] { get { if (ReferenceEquals(key, null)) { throw new ArgumentNullException("key"); } if (key is TKey) { return this[(TKey)key]; } return null; } set { throw new NotSupportedException(); } } TValue IDictionary.this[TKey key] { get { return this[key]; } set { throw new NotSupportedException(); } } public TValue this[TKey key] { get { return _wrapped[key]; } } public bool ContainsKey(TKey key) { return _wrapped.ContainsKey(key); } public IEnumerator> GetEnumerator() { return _wrapped.GetEnumerator(); } void ICollection.CopyTo(Array array, int index) { ((ICollection)_wrapped).CopyTo(array, index); } void ICollection>.Add(KeyValuePair item) { GC.KeepAlive(item); throw new NotSupportedException(); } void ICollection>.Clear() { throw new NotSupportedException(); } bool ICollection>.Contains(KeyValuePair item) { return _wrapped.Contains(item); } void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { _wrapped.CopyTo(array, arrayIndex); } bool ICollection>.Remove(KeyValuePair item) { GC.KeepAlive(item); throw new NotSupportedException(); } void IDictionary.Add(object key, object value) { throw new NotSupportedException(); } void IDictionary.Clear() { throw new NotSupportedException(); } bool IDictionary.Contains(object key) { if (ReferenceEquals(key, null)) { throw new ArgumentNullException("key"); } return key is TKey && ContainsKey((TKey)key); } IDictionaryEnumerator IDictionary.GetEnumerator() { return ((IDictionary)_wrapped).GetEnumerator(); } void IDictionary.Remove(object key) { throw new NotSupportedException(); } void IDictionary.Add(TKey key, TValue value) { throw new NotSupportedException(); } bool IDictionary.Remove(TKey key) { throw new NotSupportedException(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool TryGetValue(TKey key, out TValue value) { return _wrapped.TryGetValue(key, out value); } } } #endif