Esempio n. 1
0
        /// <summary>
        /// Creates a deep copy of an object using the supplied dictionary of visited objects as
        /// a source of objects already encountered in the copy traversal. The dictionary of visited
        /// objects is used for holding objects that have already been copied, to avoid erroneous
        /// duplication of parts of the object graph.
        /// </summary>
        /// <param name="instance">The object to be copied.</param>
        /// <param name="visited">The graph of objects visited so far.</param>
        /// <returns></returns>
        private static object Clone(this object instance, VisitedGraph visited)
        {
            if (instance == null)
            {
                return(null);
            }

            Type instanceType = instance.GetType();

            if (instanceType.IsValueType || instanceType == typeof(string))
            {
                return(instance); // Value types and strings are immutable
            }
            else if (instanceType.IsArray)
            {
                int   length = ((Array)instance).Length;
                Array copied = (Array)Activator.CreateInstance(instanceType, length);
                visited.Add(instance, copied);
                for (int i = 0; i < length; ++i)
                {
                    copied.SetValue(((Array)instance).GetValue(i).Clone(visited), i);
                }
                return(copied);
            }
            else
            {
                return(Clone(instance, visited, InstanceCreator.GetInstance(instanceType)));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a copy of the object.
        /// </summary>
        /// <param name="instance">The object to be copied.</param>
        /// <returns>A deep copy of the object.</returns>
        public static object Copy(this object instance)
        {
            if (instance == null)
            {
                return(null);
            }

            var copy = InstanceCreator.GetInstance(instance.GetType());

            Copy(instance, copy);
            return(copy);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a copy of the object.
        /// </summary>
        /// <param name="instance">The object to be copied.</param>
        /// <returns>A deep copy of the object.</returns>
        public static object Copy(this object instance)
        {
            if (instance == null)
            {
                return(null);
            }

            var copy = InstanceCreator.GetInstance(instance.GetType());

            Copy(instance, copy);
            //var ser1 = Serialize(instance);
            //var ser2 = Serialize(copy);
            //if (!CompareMemoryStreams(ser1, ser2))
            //{
            //    var co = new CompareObjects();
            //    co.CompareChildren = true;
            //    co.ComparePrivateFields = true;
            //    co.ComparePrivateProperties = true;
            //    co.Compare(instance, copy);
            //    throw new InvalidOperationException(co.DifferencesString);
            //}
            return(copy);
        }