Пример #1
0
        /// <summary>
        /// Copies the base objects data to the specified target object.
        /// </summary>
        /// <param name="baseObj"></param>
        /// <param name="targetObj"></param>
        /// <param name="fields"></param>
        public void CopyObjectTo <T>(T baseObj, T targetObj, IEnumerable <FieldInfo> fields = null)
        {
            if (fields == null)
            {
                Type objType = baseObj.GetType();
                if (!this.DoesUnwrapType(objType))
                {
                    return;
                }

                // IClonables
                if (baseObj is ICloneable)
                {
                    (baseObj as ICloneable).CopyDataTo(targetObj, this);
                    return;
                }

                // ISurrogate
                ISurrogate surrogate = this.GetSurrogateFor(objType);
                if (surrogate != null)
                {
                    surrogate.RealObject = baseObj;
                    surrogate.CopyDataTo(targetObj, this);
                    return;
                }

                fields = objType.GetAllFields(ReflectionHelper.BindInstanceAll);
            }
            foreach (FieldInfo f in fields)
            {
                f.SetValue(targetObj, this.RequestObjectClone(f.GetValue(baseObj)));
            }
        }
Пример #2
0
        private object CloneObject(object baseObj)
        {
            Type objType = baseObj.GetType();

            if (!this.DoesUnwrapType(objType))
            {
                return(baseObj);
            }

            // IClonables
            if (baseObj is ICloneable)
            {
                object copy = objType.CreateInstanceOf() ?? objType.CreateInstanceOf(true);
                if (objType.IsClass)
                {
                    this.RegisterObjectClone(baseObj, copy);
                }
                (baseObj as ICloneable).CopyDataTo(copy, this);
                return(copy);
            }

            // ISurrogate
            ISurrogate surrogate = this.GetSurrogateFor(objType);

            if (surrogate != null)
            {
                surrogate.RealObject = baseObj;
                object copy = surrogate.CreateTargetObject(this);
                if (objType.IsClass)
                {
                    this.RegisterObjectClone(baseObj, copy);
                }
                surrogate.CopyDataTo(copy, this);
                return(copy);
            }

            // Shallow types, cloned by assignment
            if (objType.IsDeepByValueType())
            {
                return(baseObj);
            }
            // Arrays
            else if (objType.IsArray)
            {
                Array baseArray = (Array)baseObj;
                Type  elemType  = objType.GetElementType();
                int   length    = baseArray.Length;
                Array copy      = Array.CreateInstance(elemType, length);
                this.RegisterObjectClone(baseObj, copy);

                bool unwrap = this.DoesUnwrapType(elemType);
                if (unwrap)
                {
                    for (int i = 0; i < length; ++i)
                    {
                        copy.SetValue(this.RequestObjectClone(baseArray.GetValue(i)), i);
                    }
                }
                else if (!elemType.IsValueType)
                {
                    for (int i = 0; i < length; ++i)
                    {
                        object obj = baseArray.GetValue(i);
                        copy.SetValue(this.GetRegisteredObjectClone(obj) ?? obj, i);
                    }
                }
                else
                {
                    baseArray.CopyTo(copy, 0);
                }

                return(copy);
            }
            // Reference types / complex objects
            else
            {
                object copy = objType.CreateInstanceOf() ?? objType.CreateInstanceOf(true);
                if (objType.IsClass)
                {
                    this.RegisterObjectClone(baseObj, copy);
                }

                this.CopyObjectTo(baseObj, copy, objType.GetAllFields(ReflectionHelper.BindInstanceAll));

                return(copy);
            }
        }