internal static object CloneClassInternal(object obj, DeepCloneState state)
        {
            if (obj == null)
            {
                return(null);
            }

            var cloner = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(obj.GetType(), t => GenerateCloner(t, true));

            // safe ojbect
            if (cloner == null)
            {
                return(obj);
            }

            // loop
            var knownRef = state.GetKnownRef(obj);

            if (knownRef != null)
            {
                return(knownRef);
            }

            return(cloner(obj, state));
        }
        private static object CloneClassRoot(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            // we can receive an poco objects which is faster to copy in shallow way if possible
            var type = obj.GetType();

            // 200ms
            if (DeepClonerSafeTypes.IsClassSafe(type))
            {
                return(ShallowObjectCloner.CloneObject(obj));
            }
            // 350ms
            var cloner = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(type, t => GenerateCloner(t, true));

            if (cloner == null)
            {
                return(obj);
            }

            // 200ms
            return(cloner(obj, new DeepCloneState()));
        }
        public static object CloneObjectTo(object objFrom, object objTo, bool isDeep)
        {
            if (objTo == null)
            {
                return(null);
            }

            if (objFrom == null)
            {
                throw new ArgumentNullException("objFrom", "Cannot copy null object to another");
            }
            var type = objFrom.GetType();

            if (!type.IsInstanceOfType(objTo))
            {
                throw new InvalidOperationException("From object should be derived from From object, but From object has type " + objFrom.GetType().FullName + " and to " + objTo.GetType().FullName);
            }
            if (objFrom is string)
            {
                throw new InvalidOperationException("It is forbidden to clone strings");
            }
            var cloner = (Func <object, object, DeepCloneState, object>)(isDeep
                                ? DeepClonerCache.GetOrAddDeepClassTo(type, t => ClonerToExprGenerator.GenerateClonerInternal(t, true))
                                : DeepClonerCache.GetOrAddShallowClassTo(type, t => ClonerToExprGenerator.GenerateClonerInternal(t, false)));

            if (cloner == null)
            {
                return(objTo);
            }
            return(cloner(objFrom, objTo, new DeepCloneState()));
        }
Example #4
0
 /// <summary>
 /// Purpose of this method is testing variants
 /// </summary>
 internal static void SwitchTo(bool isSafe)
 {
     DeepClonerCache.ClearCache();
     if (isSafe)
     {
         _instance = new ShallowSafeObjectCloner();
     }
     else
     {
         _instance = _unsafeInstance;
     }
 }
Example #5
0
        private static object CloneClassRoot(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var cloner = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(obj.GetType(), t => GenerateCloner(t, true));

            // null -> should return same type
            if (cloner == null)
            {
                return(obj);
            }

            return(cloner(obj, new DeepCloneState()));
        }
 internal static Func <T, DeepCloneState, T> GetClonerForValueType <T>()
 {
     return((Func <T, DeepCloneState, T>)DeepClonerCache.GetOrAddStructAsObject(typeof(T), t => GenerateCloner(t, false)));
 }
        internal static object CloneClassInternal(object obj, DeepCloneState state)
        {
            if (obj == null)
            {
                return(null);
            }
            Func <object, DeepCloneState, object> func = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(obj.GetType(), (Type t) => GenerateCloner(t, true));

            if (func == null)
            {
                return(obj);
            }
            object knownRef = state.GetKnownRef(obj);

            if (knownRef != null)
            {
                return(knownRef);
            }
            return(func(obj, state));
        }
        private static object CloneClassRoot(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            Type type = obj.GetType();

            if (DeepClonerSafeTypes.CanNotDeepCopyClass(type))
            {
                return(ShallowObjectCloner.CloneObject(obj));
            }
            Func <object, DeepCloneState, object> func = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(type, (Type t) => GenerateCloner(t, true));

            if (func == null)
            {
                return(obj);
            }
            return(func(obj, new DeepCloneState()));
        }