Esempio n. 1
0
        object IBCopier.Copy(object t, BCopierSettings copierSettings)
        {
            if (t == null)
            {
                return(null);
            }

            return(Copy(t, new BCopierContext(), copierSettings));
        }
        object IBCopierInternal.Copy(object t, BCopierContext copierContext, BCopierSettings bCopierSettings)
        {
            Type concrete = t.GetType();

            // check the copiers local cache
            if (!PotentialTypeCopiers.TryGetValue(concrete, out IBCopierInternal copier))
            {
                copier = BDeepCopyProvider.GetOrCreate(concrete);
                PotentialTypeCopiers.Add(concrete, copier);
            }

            return(copier.Copy(t, copierContext, bCopierSettings));
        }
Esempio n. 3
0
        private object Copy(object t, BCopierContext copierContext, BCopierSettings copierSettings)
        {
            if (t == null)
            {
                return(null);
            }

            if (copierContext.TryGetCached(t, out object cached))
            {
                return(cached);
            }

            object emptyCopy = CreateEmptyCopy(t);

            copierContext.Register(t, emptyCopy);

            return(Copy(t, emptyCopy, copierContext, copierSettings));
        }
        private bool TryCustomOverride(BCopierSettings copierSettings, MemberInfo memberInfo, ref object originalVal, IBCopierInternal copier, Dictionary <Type, CopierSettingAttribute> overrides, out object copy)
        {
            // look for overrides
            foreach (KeyValuePair <Type, CopierSettingAttribute> kv in overrides)
            {
                // custom copier overrides
                for (int c = 0; c < copierSettings.CopierOverrides.Count; c++)
                {
                    IBCopierOverride curr = copierSettings.CopierOverrides[c];

                    if (curr.AttributeType == kv.Key)
                    {
                        copy = curr.CopyOverride(in originalVal, memberInfo, kv.Value);
                        return(true);
                    }
                }
            }

            copy = null;
            return(false);
        }
        internal override object Copy(object t, object emptyCopy, BCopierContext copierContext, BCopierSettings copierSettings)
        {
            for (int i = 0; i < MemberCount; i++)
            {
                MemberInfo              mem    = MemberInfos[i];
                Func <object, object>   getter = Getters[i];
                Action <object, object> setter = Setters[i];
                IBCopierInternal        copier = Copiers[i];
                Dictionary <Type, CopierSettingAttribute> customOverrides = BCopierOverrides[i];
                List <Tuple <CopierSettingAttribute, IBCopierOverrideInternal> > internalOverrides = InternalBCopierOverrides[i];

                object originalVal = getter.Invoke(t);
                object copy        = null;

                // if we found an internal override
                // then we apply it and go next
                if (TryInternalOverride(internalOverrides, mem, ref originalVal, copier, out copy))
                {
                    setter.Invoke(emptyCopy, copy);
                    continue;
                }

                // if no copier setting we use default
                if (copierSettings == null)
                {
                    copy = copier.Copy(originalVal, copierContext, copierSettings);
                }

                // try to check if we can apply an override
                // if not , then we use the default copy
                else if (!TryCustomOverride(copierSettings, mem, ref originalVal, copier, customOverrides, out copy))
                {
                    copy = copier.Copy(originalVal, copierContext, copierSettings);
                }

                // copy from original and assign to new instance
                setter.Invoke(emptyCopy, copy);
            }

            return(emptyCopy);
        }
        internal override object Copy(object t, object instance, BCopierContext copierContext, BCopierSettings copierSettings)
        {
            Array casted         = (Array)t;
            Array castedInstance = (Array)instance;

            // cached locals
            object           curr     = null;
            Type             currType = null;
            IBCopierInternal c        = null;
            object           copy     = null;

            for (int i = 0; i < casted.Length; i++)
            {
                curr     = casted.GetValue(i);
                currType = curr.GetType();

                c = GetElementCopier(currType);

                copy = c.Copy(curr, copierContext, copierSettings);

                castedInstance.SetValue(copy, i);
            }

            return(castedInstance);
        }
Esempio n. 7
0
 public object Copy(object t, BCopierSettings bCopierSettings = null)
 {
     return(t);
 }
Esempio n. 8
0
 public object Copy(object t, BCopierContext copierContext, BCopierSettings bCopierSettings)
 {
     return(t);
 }
Esempio n. 9
0
 public T Copy(T t, BCopierSettings bCopierSettings = null)
 {
     return((T)Copier.Copy(t, bCopierSettings));
 }
Esempio n. 10
0
 T IBCopier <T> .Copy(T t, BCopierSettings bCopierSettings = null)
 {
     return(Copy(t, bCopierSettings));
 }
Esempio n. 11
0
 object IBCopier.Copy(object t, BCopierSettings bCopierSettings)
 {
     return(Copier.Copy(t, bCopierSettings));
 }
Esempio n. 12
0
 internal abstract object Copy(object t, object emptyCopy, BCopierContext copierContext, BCopierSettings copierSettings);
Esempio n. 13
0
 object IBCopierInternal.Copy(object t, BCopierContext copierContext, BCopierSettings copierSettings)
 {
     return(Copy(t, copierContext, copierSettings));
 }
Esempio n. 14
0
        internal override object Copy(object t, object instance, BCopierContext copierContext, BCopierSettings copierSettings)
        {
            // TODO : optimize by looking for contructor with capacity first
            // DONE
            IList casted         = (IList)t;
            IList castedInstance = (IList)instance;

            // cached locals
            object           curr     = null;
            Type             currType = null;
            IBCopierInternal c        = null;
            object           copy     = null;

            for (int i = 0; i < casted.Count; i++)
            {
                curr     = casted[i];
                currType = curr.GetType();

                c = GetElementCopier(currType);

                copy = c.Copy(curr, copierContext, copierSettings);

                castedInstance.Add(copy);
            }

            return(castedInstance);
        }
 object IBCopier.Copy(object t, BCopierSettings bCopierSettings)
 {
     return(((IBCopierInternal)this).Copy(t, new BCopierContext(), bCopierSettings));
 }
Esempio n. 16
0
        internal override object Copy(object t, object instance, BCopierContext copierContext, BCopierSettings copierSettings)
        {
            // TODO : optimize by looking for contructor with capacity first
            // DONE
            IDictionary casted         = (IDictionary)t;
            IDictionary castedInstance = (IDictionary)instance;

            object[] keys = new object[casted.Count];
            object[] vals = new object[casted.Count];

            casted.Keys.CopyTo(keys, 0);
            casted.Values.CopyTo(vals, 0);

            // cached locals
            object           curr     = null;
            IBCopierInternal c        = null;
            Type             currType = null;
            object           copyKey  = null;
            object           copyVal  = null;

            for (int i = 0; i < casted.Count; i++)
            {
                // copy key
                curr     = keys[i];
                currType = curr.GetType();

                c = GetKeyCopier(currType);

                copyKey = c.Copy(curr, copierContext, copierSettings);

                // copy key
                curr     = vals[i];
                currType = curr.GetType();

                c = GetValueCopier(currType);

                copyVal = c.Copy(curr, copierContext, copierSettings);

                castedInstance.Add(copyKey, copyVal);
            }

            return(castedInstance);
        }