public static T CloneObject <T>(T obj)
        {
            // this is faster than typeof(T).IsValueType
            if (obj is ValueType)
            {
                if (typeof(T) == obj.GetType())
                {
                    return(obj);
                }

                // we're here so, we clone value type obj as object type T
                // so, we need to copy it, bcs we have a reference, not real object.
                return((T)ShallowObjectCloner.CloneObject(obj));
            }

            if (ReferenceEquals(obj, null))
            {
                return((T)(object)null);
            }

            if (DeepClonerSafeTypes.CanReturnSameObject(obj.GetType()))
            {
                return(obj);
            }

            return((T)ShallowObjectCloner.CloneObject(obj));
        }
        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()));
        }
Exemple #3
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;
     }
 }
Exemple #4
0
        public static T CloneObject <T>(T obj)
        {
            // this is faster than typeof(T).IsValueType
            if (obj is ValueType)
            {
                if (typeof(T) == obj.GetType())
                {
                    return(obj);
                }
            }

            return((T)ShallowObjectCloner.CloneObject(obj));
        }
        private static object GenerateCloner(Type t, bool asObject)
        {
#if !NETCORE
            if (ShallowObjectCloner.IsSafeVariant())
            {
                return(DeepClonerExprGenerator.GenerateClonerInternal(t, asObject));
            }
            else
            {
                return(DeepClonerMsilGenerator.GenerateClonerInternal(t, asObject));
            }
#else
            return(DeepClonerExprGenerator.GenerateClonerInternal(t, asObject));
#endif
        }
Exemple #6
0
        static ShallowObjectCloner()
        {
#if !NETCORE
            _unsafeInstance = GenerateUnsafeCloner();
            _instance       = _unsafeInstance;
            try
            {
                _instance.DoCloneObject(new object());
            }
            catch (Exception)
            {
                // switching to safe
                _instance = new ShallowSafeObjectCloner();
            }
#else
            _instance = new ShallowSafeObjectCloner();
            // no unsafe variant for core
            _unsafeInstance = _instance;
#endif
        }
Exemple #7
0
        private static object GenerateCloner(Type t, bool asObject)
        {
            if (DeepClonerSafeTypes.CanReturnSameObject(t) && (asObject && !t.IsValueType()))
            {
                return(null);
            }

#if !NETCORE
            if (ShallowObjectCloner.IsSafeVariant())
            {
                return(DeepClonerExprGenerator.GenerateClonerInternal(t, asObject));
            }
            else
            {
                return(DeepClonerMsilGenerator.GenerateClonerInternal(t, asObject));
            }
#else
            return(DeepClonerExprGenerator.GenerateClonerInternal(t, asObject));
#endif
        }
        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()));
        }
Exemple #9
0
 static ShallowObjectCloner()
 {
     _instance       = new ShallowSafeObjectCloner();
     _unsafeInstance = _instance;
 }