Exemplo n.º 1
0
        /// <summary>
        /// Returns a read-only view of the <see cref="SortedDictionaryEx{TKey, TValue}"/>.
        /// </summary>
        /// <returns>
        /// A read-only wrapper around the <see cref="SortedDictionaryEx{TKey, TValue}"/>.</returns>
        /// <remarks><para>
        /// Attempting to modify the read-only wrapper returned by <b>AsReadOnly</b> will raise a
        /// <see cref="NotSupportedException"/>. Note that the original collection may still change,
        /// and any such changes will be reflected in the read-only view.
        /// </para><para>
        /// <b>AsReadOnly</b> buffers the newly created read-only wrapper when the method is first
        /// called, and returns the buffered value on subsequent calls.</para></remarks>

        public SortedDictionaryEx <TKey, TValue> AsReadOnly()
        {
            if (ReadOnlyWrapper == null)
            {
                ReadOnlyWrapper = new SortedDictionaryEx <TKey, TValue>(this, true);
            }

            return(ReadOnlyWrapper);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SortedDictionaryEx{TKey, TValue}"/> class
        /// that is a read-only view of the specified instance.</summary>
        /// <param name="dictionary">
        /// The <see cref="SortedDictionaryEx{TKey, TValue}"/> collection that provides the initial
        /// value for the <see cref="InnerDictionary"/> field.</param>
        /// <param name="readOnly">
        /// The initial value for the <see cref="IsReadOnly"/> property. This argument must be
        /// <c>true</c>.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="dictionary"/> is a null reference.</exception>
        /// <exception cref="InvalidOperationException">
        /// <paramref name="readOnly"/> is <c>false</c>.</exception>
        /// <remarks>
        /// This constructor is used to create a read-only wrapper around an existing collection.
        /// The new instance shares the data of the specified <paramref name="dictionary"/>.
        /// </remarks>

        protected SortedDictionaryEx(SortedDictionaryEx <TKey, TValue> dictionary, bool readOnly)
        {
            if (dictionary == null)
            {
                ThrowHelper.ThrowArgumentNullException("dictionary");
            }

            if (!readOnly)
            {
                ThrowHelper.ThrowArgumentExceptionWithFormat(
                    "readOnly", Strings.ArgumentEquals, false);
            }

            InnerDictionary = dictionary.InnerDictionary;
            ReadOnlyFlag    = readOnly;
            ReadOnlyWrapper = this;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a deep copy of the <see cref="SortedDictionaryEx{TKey, TValue}"/>.</summary>
        /// <returns>
        /// A deep copy of the <see cref="SortedDictionaryEx{TKey, TValue}"/>.</returns>
        /// <exception cref="InvalidCastException">
        /// <typeparamref name="TValue"/> does not implement <see cref="ICloneable"/>.</exception>
        /// <remarks><para>
        /// <b>Copy</b> is similar to <see cref="Clone"/> but creates a deep copy the <see
        /// cref="SortedDictionaryEx{TKey, TValue}"/> by invoking <see cref="ICloneable.Clone"/> on
        /// all <typeparamref name="TValue"/> values. The <typeparamref name="TKey"/> keys are
        /// always duplicated by a shallow copy.
        /// </para><para>
        /// <b>Copy</b> does not preserve the values of the <see cref="IsFixedSize"/> and <see
        /// cref="IsReadOnly"/> properties.</para></remarks>

        public SortedDictionaryEx <TKey, TValue> Copy()
        {
            SortedDictionaryEx <TKey, TValue> copy = new SortedDictionaryEx <TKey, TValue>();

            foreach (KeyValuePair <TKey, TValue> pair in InnerDictionary)
            {
                TValue value = pair.Value;

                ICloneable cloneable = (ICloneable)value;
                if (cloneable != null)
                {
                    value = (TValue)cloneable.Clone();
                }

                copy.InnerDictionary.Add(pair.Key, value);
            }

            return(copy);
        }