using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.Serialization; using System.Text; namespace System.Collections.ObjectModel { [Serializable(), DebuggerDisplay("Count = {Count}")] public class ReadOnlyDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback { private IDictionary dictionary; public ReadOnlyDictionary(IDictionary dictionary) { this.dictionary = dictionary; } #region IDictionary Members public ICollection Keys { get { return this.dictionary.Keys; } } public ICollection Values { get { return this.dictionary.Values; } } public TValue this[TKey key] { get { return this.dictionary[key]; } set { throw new NotSupportedException(); } } public void Add(TKey key, TValue value) { throw new NotSupportedException(); } public bool Remove(TKey key) { throw new NotSupportedException(); } public bool ContainsKey(TKey key) { return this.dictionary.ContainsKey(key); } public bool TryGetValue(TKey key, out TValue value) { return this.dictionary.TryGetValue(key, out value); } #endregion #region ICollection> Members public int Count { get { return this.dictionary.Count; } } public bool IsReadOnly { get { return true; } } public void Add(KeyValuePair item) { throw new NotSupportedException(); } public bool Remove(KeyValuePair item) { throw new NotSupportedException(); } public void Clear() { throw new NotSupportedException(); } public bool Contains(KeyValuePair item) { return this.dictionary.Contains(item); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { this.dictionary.CopyTo(array, arrayIndex); } #endregion #region IEnumerable> Members public IEnumerator> GetEnumerator() { return ((IEnumerable>)this.dictionary).GetEnumerator(); } #endregion #region IDictionary Members ICollection IDictionary.Keys { get { return ((IDictionary)this.dictionary).Keys; } } ICollection IDictionary.Values { get { return ((IDictionary)this.dictionary).Values; } } bool IDictionary.IsFixedSize { get { return ((IDictionary)this.dictionary).IsFixedSize; } } bool IDictionary.IsReadOnly { get { return true; } } object IDictionary.this[object key] { get { return ((IDictionary)this.dictionary)[key]; } set { throw new NotSupportedException(); } } void IDictionary.Add(object key, object value) { throw new NotSupportedException(); } void IDictionary.Remove(object key) { throw new NotSupportedException(); } void IDictionary.Clear() { throw new NotSupportedException(); } bool IDictionary.Contains(object key) { return ((IDictionary)this.dictionary).Contains(key); } IDictionaryEnumerator IDictionary.GetEnumerator() { return ((IDictionary)this.dictionary).GetEnumerator(); } #endregion #region ICollection Members void ICollection.CopyTo(Array array, int index) { ((ICollection)this.dictionary).CopyTo(array, index); } int ICollection.Count { get { return ((ICollection)this.dictionary).Count; } } bool ICollection.IsSynchronized { get { return ((ICollection)this.dictionary).IsSynchronized; } } object ICollection.SyncRoot { get { return ((ICollection)this.dictionary).SyncRoot; } } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)this.dictionary).GetEnumerator(); } #endregion #region ISerializable Members void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { ((ISerializable)this.dictionary).GetObjectData(info, context); } #endregion #region IDeserializationCallback Members void IDeserializationCallback.OnDeserialization(object sender) { ((IDeserializationCallback)this.dictionary).OnDeserialization(sender); } #endregion } }