Ejemplo n.º 1
0
 internal static void SwitchTo(bool isSafe)
 {
     DeepClonerCache.ClearCache();
     if (isSafe)
     {
         _instance = new ShallowSafeObjectCloner();
     }
     else
     {
         _instance = _unsafeInstance;
     }
 }
Ejemplo n.º 2
0
        internal static object CloneClassInternal(object obj, DeepCloneState state)
        {
            if (obj == null)
            {
                return(null);
            }
            Func <object, DeepCloneState, object> cloner = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(obj.GetType(), (Type t) => GenerateCloner(t, asObject: true));

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

            if (knownRef != null)
            {
                return(knownRef);
            }
            return(cloner(obj, state));
        }
Ejemplo n.º 3
0
        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");
            }
            Type 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");
            }
            Func <object, object, DeepCloneState, object> cloner = (Func <object, object, DeepCloneState, object>)(isDeep ? DeepClonerCache.GetOrAddDeepClassTo(type, (Type t) => ClonerToExprGenerator.GenerateClonerInternal(t, isDeepClone: true)) : DeepClonerCache.GetOrAddShallowClassTo(type, (Type t) => ClonerToExprGenerator.GenerateClonerInternal(t, isDeepClone: false)));

            if (cloner == null)
            {
                return(objTo);
            }
            return(cloner(objFrom, objTo, new DeepCloneState()));
        }
Ejemplo n.º 4
0
        private static object CloneClassRoot(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            Func <object, DeepCloneState, object> cloner = (Func <object, DeepCloneState, object>)DeepClonerCache.GetOrAddClass(obj.GetType(), (Type t) => GenerateCloner(t, asObject: true));

            if (cloner == null)
            {
                return(obj);
            }
            return(cloner(obj, new DeepCloneState()));
        }
Ejemplo n.º 5
0
 internal static Func <T, DeepCloneState, T> GetClonerForValueType <T>()
 {
     return((Func <T, DeepCloneState, T>)DeepClonerCache.GetOrAddStructAsObject(typeof(T), (Type t) => GenerateCloner(t, asObject: false)));
 }