//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------ namespace System.ServiceModel { using System.Collections; using System.Collections.Generic; [Serializable] class SerializableReadOnlyDictionary : IDictionary { static IDictionary empty; IDictionary dictionary; public SerializableReadOnlyDictionary(IDictionary dictionary) : this(dictionary, true) { } public SerializableReadOnlyDictionary(IDictionary dictionary, bool makeCopy) { if (makeCopy) { this.dictionary = new Dictionary(dictionary); } else { this.dictionary = dictionary; } } public SerializableReadOnlyDictionary(params KeyValuePair[] entries) { this.dictionary = new Dictionary(entries.Length); foreach (KeyValuePair pair in entries) { this.dictionary.Add(pair); } } public static IDictionary Empty { get { if (empty == null) { empty = new SerializableReadOnlyDictionary(new Dictionary(0), false); } return empty; } } public int Count { get { return this.dictionary.Count; } } public bool IsReadOnly { get { return true; } } public ICollection Keys { get { return this.dictionary.Keys; } } public ICollection Values { get { return this.dictionary.Values; } } public V this[K key] { get { return this.dictionary[key]; } set { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); } } public void Add(K key, V value) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); } public void Add(KeyValuePair item) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); } public void Clear() { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); } public bool Contains(KeyValuePair item) { return this.dictionary.Contains(item); } public bool ContainsKey(K key) { return this.dictionary.ContainsKey(key); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { this.dictionary.CopyTo(array, arrayIndex); } public IEnumerator> GetEnumerator() { return this.dictionary.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } public bool Remove(K key) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); } public bool Remove(KeyValuePair item) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); } public bool TryGetValue(K key, out V value) { return this.dictionary.TryGetValue(key, out value); } } }