Ejemplo n.º 1
0
        ///<summary>
        /// Creates a new object that is a copy of the current instance.
        ///</summary>
        ///<returns>
        /// A new object that is a copy of this instance.
        ///</returns>
        public object Clone( )
        {
            CloneableDictionary <TKey, TValue> result =
                new CloneableDictionary <TKey, TValue> ();
            Type keyType     = typeof(TKey);
            Type valueType   = typeof(TValue);
            bool cloneKeys   = false;
            bool cloneValues = false;

            if (!keyType.IsValueType)
            {
                if (keyType.IsAssignableFrom(typeof(ICloneable)))
                {
                    throw new ArgumentException(keyType.Name);
                }
                cloneKeys = true;
            }
            if (!valueType.IsValueType)
            {
                if (valueType.IsAssignableFrom(typeof(ICloneable)))
                {
                    throw new ArgumentException(valueType.Name);
                }
                cloneValues = true;
            }

            foreach (KeyValuePair <TKey, TValue> pair in this)
            {
                TKey   keyCopy   = pair.Key;
                TValue valueCopy = pair.Value;
                if (cloneKeys)
                {
                    keyCopy = (TKey)((ICloneable)pair.Key).Clone();
                }
                if (cloneValues)
                {
                    valueCopy = (TValue)((ICloneable)pair.Value).Clone();
                }
                result.Add
                (
                    keyCopy,
                    valueCopy);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <inheritdoc cref="ICloneable.Clone" />
        public object Clone()
        {
            CloneableDictionary <TKey, TValue> result
                = new CloneableDictionary <TKey, TValue>();

            foreach (KeyValuePair <TKey, TValue> pair in this)
            {
                result.Add(pair.Key, pair.Value);
            }

            //Type keyType = typeof(TKey);
            //Type valueType = typeof(TValue);
            //bool cloneKeys = false;
            //bool cloneValues = false;

            //if (!TypeUtility.IsValueType(keyType))
            //{
            //    if (keyType.Bridge().IsAssignableFrom(typeof(ICloneable)))
            //    {
            //        Log.Error
            //            (
            //                "CloneableDictionary::Clone: "
            //                + "type "
            //                + keyType.FullName
            //                + " is not cloneable"
            //            );

            //        throw new ArgumentException(keyType.Name);
            //    }
            //    cloneKeys = true;
            //}
            //if (!TypeUtility.IsValueType(valueType))
            //{
            //    if (valueType.Bridge().IsAssignableFrom(typeof(ICloneable)))
            //    {
            //        Log.Error
            //        (
            //            "CloneableDictionary::Clone: "
            //            + "type "
            //            + valueType.FullName
            //            + " is not cloneable"
            //        );

            //        throw new ArgumentException(valueType.Name);
            //    }
            //    cloneValues = true;
            //}

            //foreach (KeyValuePair<TKey, TValue> pair in this)
            //{
            //    TKey keyCopy = pair.Key;
            //    TValue valueCopy = pair.Value;
            //    if (cloneKeys)
            //    {
            //        keyCopy = (TKey)((ICloneable)pair.Key).Clone();
            //    }
            //    if (cloneValues
            //        && !ReferenceEquals(valueCopy, null))
            //    {
            //        valueCopy = (TValue)((ICloneable)pair.Value).Clone();
            //    }
            //    result.Add(keyCopy, valueCopy);
            //}

            return(result);
        }