Anyone know how to create a read-only generic dictionary?

Last post 07-16-2007, 6:30 AM by pejvan. 3 replies.
Sort Posts: Previous Next
  •  09-29-2006, 11:20 AM Post number 2263

    Anyone know how to create a read-only generic dictionary?

    Hi All,


    As above really. What I need to do is either create, or make an existing, IDictionary<TKey, TValue> read-only. I notice that Dictionary<TKey, TValue> implements the IsReadOnly property from the System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>> interface but I can't find any way that I could make a Dictionary<TKey, TValue> that would return true for this property and complain if you tried to modify it.

    I guess what I could do is subclass Dictionary<TKey, TValue> but I'm not sure that's workable since I don't think some of the key members are actually virtual, which leaves creating a class called ReadOnlyDictionary<TKey, TValue> which would simply wrap another IDictionary<TKey, TValue> implementation... unless of course there's something simpler I can do?

    Anyone got any ideas?


    Many thanks,
    Bart




    Generation X.1
  •  10-13-2006, 2:18 AM Post number 2673 in reply to post number 2263

    Re: Anyone know how to create a read-only generic dictionary?

    Hi Bart,

    I would create a wrapper around Dictionary<> and implement readonly property. And you are correct, you can't force Dictionary to be readonly.


    Miha Markic [MVP C#, INETA Country Leader for Slovenia]
    Blog:http://cs.rthand.com/blogs/blog_with_righthand
    Righthand .net consulting and software development
    http://www.rthand.com
  •  12-11-2006, 2:22 PM Post number 8776 in reply to post number 2263

    Re: Anyone know how to create a read-only generic dictionary?

    I am putting this code in public domain

    /// <summary>
        /// Wraps an existing ICollection as read only, following the pattern of
        /// ReadOnlyCollection to simply no-op modifying functions intead of throwing
        /// Exceptions.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        [Serializable]
        public class ReadOnlyICollection<T> : ICollection<T>
        {
            /// <summary>
            ///
            /// </summary>
            private ICollection<T> coll;

            /// <summary>
            /// Initializes a new instance of the <see cref="ReadOnlyICollection&lt;T&gt;"/> class.
            /// </summary>
            /// <param name="collectionToWrap">The collection to wrap.</param>
            public ReadOnlyICollection(ICollection<T> collectionToWrap)
            {
                coll = collectionToWrap;
            }

            /// <summary>
            /// Returned a read only wrapper around the collectionToWrap.
            /// </summary>
            /// <param name="collectionToWrap">The collection to wrap.</param>
            /// <returns></returns>
            public static ReadOnlyICollection<T> AsReadOnly(ICollection<T> collectionToWrap)
            {
                return new ReadOnlyICollection<T>(collectionToWrap);
            }

            /// <summary>
            /// Add does not change a ReadOnlyICollection
            /// </summary>
            /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
            public void Add(T item)
            {
            }

            /// <summary>
            /// Clear does not change a ReadOnlyICollection
            /// </summary>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
            public void Clear()
            {
            }

            /// <summary>
            /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value.
            /// </summary>
            /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
            /// <returns>
            /// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
            /// </returns>
            public bool Contains(T item)
            {
                return coll.Contains(item);
            }

            /// <summary>
            /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
            /// </summary>
            /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
            /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
            /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
            /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
            /// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>
            public void CopyTo(T[] array, int arrayIndex)
            {
                coll.CopyTo(array, arrayIndex);
            }

            /// <summary>
            /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>
            public int Count
            {
                get
                {
                    return coll.Count;
                }
            }

            /// <summary>
            /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.
            /// </summary>
            /// <value></value>
            /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>
            public bool IsReadOnly
            {
                get
                {
                    return true;
                }
            }

            /// <summary>
            /// Remove does not change a ReadOnlyICollection
            /// </summary>
            /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
            /// <returns>
            /// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
            /// </returns>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
            public bool Remove(T item)
            {
                return false;
            }

            /// <summary>
            /// Returns an enumerator that iterates through the collection.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
            /// </returns>
            public IEnumerator<T> GetEnumerator()
            {
                return coll.GetEnumerator();
            }

            /// <summary>
            /// Returns an enumerator that iterates through a collection.
            /// </summary>
            /// <returns>
            /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
            /// </returns>
            IEnumerator IEnumerable.GetEnumerator()
            {
                return coll.GetEnumerator();
            }
        }

        /// <summary>
        /// Represents a read only wrapper around a generic IDictionary. The design pattern
        /// mirrors ReadOnlyCollection, and follows the apparent pattern that write operations
        /// do not throw an exception, but simply make no change to the underlying collection.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        [Serializable]
        public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
        {
            /// <summary>
            ///
            /// </summary>
            private IDictionary<TKey, TValue> dict;

            /// <summary>
            ///
            /// </summary>
            private IDictionary idict;

            /// <summary>
            /// Initializes a new instance of the <see cref="ReadOnlyDictionary&lt;TKey, TValue&gt;"/> class.
            /// </summary>
            /// <param name="dictionaryToWrap">The dictionary to wrap.</param>
            public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionaryToWrap)
            {
                dict = dictionaryToWrap;
                idict = (IDictionary)dict;
            }

            /// <summary>
            /// Returns a read only dictionary.
            /// </summary>
            /// <param name="dictionaryToWrap">The dictionary to wrap.</param>
            /// <returns></returns>
            public static ReadOnlyDictionary<TKey, TValue> AsReadOnly(IDictionary<TKey, TValue> dictionaryToWrap)
            {
                return new ReadOnlyDictionary<TKey, TValue>(dictionaryToWrap);
            }

            /// <summary>
            /// Add does not change a read only Dictionary
            /// </summary>
            /// <param name="key">The object to use as the key of the element to add.</param>
            /// <param name="value">The object to use as the value of the element to add.</param>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
            /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</exception>
            /// <exception cref="T:System.ArgumentNullException">key is null.</exception>
            public void Add(TKey key, TValue value)
            {
            }

            /// <summary>
            /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the specified key.
            /// </summary>
            /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</param>
            /// <returns>
            /// true if the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the key; otherwise, false.
            /// </returns>
            /// <exception cref="T:System.ArgumentNullException">key is null.</exception>
            public bool ContainsKey(TKey key)
            {
                return dict.ContainsKey(key);
            }

            /// <summary>
            /// Gets a read only <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>
            public ICollection<TKey> Keys
            {
                get
                {
                    return ReadOnlyICollection<TKey>.AsReadOnly(dict.Keys);
                }
            }

            /// <summary>
            /// Remove does not change a read only Dictionary
            /// </summary>
            /// <param name="key">The key of the element to remove.</param>
            /// <returns>
            /// true if the element is successfully removed; otherwise, false.  This method also returns false if key was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
            /// </returns>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
            /// <exception cref="T:System.ArgumentNullException">key is null.</exception>
            public bool Remove(TKey key)
            {
                return false;
            }

            /// <summary>
            /// Tries the get value.
            /// </summary>
            /// <param name="key">The key.</param>
            /// <param name="value">The value.</param>
            /// <returns></returns>
            public bool TryGetValue(TKey key, out TValue value)
            {
                return dict.TryGetValue(key, out value);
            }

            /// <summary>
            /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>
            public ICollection<TValue> Values
            {
                get
                {
                    return ReadOnlyICollection<TValue>.AsReadOnly(dict.Values);
                }
            }

            /// <summary>
            /// Gets the <see cref="TValue"/> with the specified key. Set
            /// does not change a read only Dictionary
            /// </summary>
            /// <value></value>
            public TValue this[TKey key]
            {
                get
                {
                    return dict[key];
                }
                set
                {
                }
            }

            /// <summary>
            /// Add does not change a read only Dictionary
            /// </summary>
            /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
            public void Add(KeyValuePair<TKey, TValue> item)
            {
            }

            /// <summary>
            /// Clear does not change a read only Dictionary.
            /// </summary>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
            public void Clear()
            {
            }

            /// <summary>
            /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value.
            /// </summary>
            /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
            /// <returns>
            /// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
            /// </returns>
            public bool Contains(KeyValuePair<TKey, TValue> item)
            {
                return dict.Contains(item);
            }

            /// <summary>
            /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
            /// </summary>
            /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
            /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
            /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
            /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
            /// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>
            public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
            {
                dict.CopyTo(array, arrayIndex);
            }

            /// <summary>
            /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>
            public int Count
            {
                get
                {
                    return dict.Count;
                }
            }

            /// <summary>
            /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.
            /// </summary>
            /// <value></value>
            /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>
            public bool IsReadOnly
            {
                get
                {
                    return true;
                }
            }

            /// <summary>
            /// Remove does not change a read only Dictionary
            /// </summary>
            /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
            /// <returns>
            /// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
            /// </returns>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
            public bool Remove(KeyValuePair<TKey, TValue> item)
            {
                return false;
            }

            /// <summary>
            /// Returns an enumerator that iterates through the collection.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
            /// </returns>
            public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
            {
                return dict.GetEnumerator();
            }

            /// <summary>
            /// Returns an enumerator that iterates through a collection.
            /// </summary>
            /// <returns>
            /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
            /// </returns>
            IEnumerator IEnumerable.GetEnumerator()
            {
                return idict.GetEnumerator();
            }

            /// <summary>
            /// Add does not change a read only Dictionary
            /// </summary>
            /// <param name="key">The <see cref="T:System.Object"></see> to use as the key of the element to add.</param>
            /// <param name="value">The <see cref="T:System.Object"></see> to use as the value of the element to add.</param>
            /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.IDictionary"></see> object. </exception>
            /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> is read-only.-or- The <see cref="T:System.Collections.IDictionary"></see> has a fixed size. </exception>
            public void Add(object key, object value)
            {
            }

            /// <summary>
            /// Determines whether the <see cref="T:System.Collections.IDictionary"></see> object contains an element with the specified key.
            /// </summary>
            /// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"></see> object.</param>
            /// <returns>
            /// true if the <see cref="T:System.Collections.IDictionary"></see> contains an element with the key; otherwise, false.
            /// </returns>
            /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
            public bool Contains(object key)
            {
                return idict.Contains(key);
            }

            /// <summary>
            /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"></see> object for the <see cref="T:System.Collections.IDictionary"></see> object.
            /// </summary>
            /// <returns>
            /// An <see cref="T:System.Collections.IDictionaryEnumerator"></see> object for the <see cref="T:System.Collections.IDictionary"></see> object.
            /// </returns>
            IDictionaryEnumerator IDictionary.GetEnumerator()
            {
                return idict.GetEnumerator();
            }

            /// <summary>
            /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"></see> object has a fixed size.
            /// </summary>
            /// <value></value>
            /// <returns>true if the <see cref="T:System.Collections.IDictionary"></see> object has a fixed size; otherwise, false.</returns>
            public bool IsFixedSize
            {
                get
                {
                    return idict.IsFixedSize;
                }
            }

            /// <summary>
            /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>
            ICollection IDictionary.Keys
            {
                get
                {
                    return idict.Keys;
                }
            }

            /// <summary>
            /// Remove does not affect a read only Dictionary
            /// </summary>
            /// <param name="key">The key of the element to remove.</param>
            /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"></see> object is read-only.-or- The <see cref="T:System.Collections.IDictionary"></see> has a fixed size. </exception>
            /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
            public void Remove(object key)
            {
            }

            /// <summary>
            /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>
            ICollection IDictionary.Values
            {
                get
                {
                    return idict.Values;
                }
            }

            /// <summary>
            /// Gets the <see cref="System.Object"/> with the specified key. Set
            /// does not affect a ReadOnlyDictionary
            /// </summary>
            /// <value></value>
            public object this[object key]
            {
                get
                {
                    return idict[key];
                }
                set
                {
                }
            }

            /// <summary>
            /// Copies the elements of the <see cref="T:System.Collections.ICollection"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
            /// </summary>
            /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
            /// <param name="index">The zero-based index in array at which copying begins.</param>
            /// <exception cref="T:System.ArgumentNullException">array is null. </exception>
            /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero. </exception>
            /// <exception cref="T:System.ArgumentException">array is multidimensional.-or- index is equal to or greater than the length of array.-or- The number of elements in the source <see cref="T:System.Collections.ICollection"></see> is greater than the available space from index to the end of the destination array. </exception>
            /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.ICollection"></see> cannot be cast automatically to the type of the destination array. </exception>
            public void CopyTo(Array array, int index)
            {
            }

            /// <summary>
            /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe).
            /// </summary>
            /// <value></value>
            /// <returns>true if access to the <see cref="T:System.Collections.ICollection"></see> is synchronized (thread safe); otherwise, false.</returns>
            public bool IsSynchronized
            {
                get
                {
                    return idict.IsSynchronized;
                }
            }

            /// <summary>
            /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.
            /// </summary>
            /// <value></value>
            /// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"></see>.</returns>
            public object SyncRoot
            {
                get
                {
                    return idict.SyncRoot;
                }
            }

            /// <summary>
            /// Runs when the entire object graph has been deserialized.
            /// </summary>
            /// <param name="sender">The object that initiated the callback. The functionality for this parameter is not currently implemented.</param>
            public void OnDeserialization(object sender)
            {
                IDeserializationCallback callback = dict as IDeserializationCallback;
                callback.OnDeserialization(sender);
            }

            /// <summary>
            /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> with the data needed to serialize the target object.
            /// </summary>
            /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> to populate with data.</param>
            /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"></see>) for this serialization.</param>
            /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                ISerializable serializable = dict as ISerializable;
                serializable.GetObjectData(info, context);
            }
        }
  •  07-16-2007, 6:30 AM Post number 33631 in reply to post number 8776

    Re: Anyone know how to create a read-only generic dictionary?

    I would just add that you need to include the following using statements:

    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Runtime.Serialization;

View as RSS news feed in XML