Example #1
0
        private object ReferenceTypeClone(Dictionary <string, IFastDeepClonerProperty> properties, Type primaryType,
                                          object objectToBeCloned, object appendToValue = null)
        {
            var identifier = objectToBeCloned.GetFastDeepClonerIdentifier();

            if (identifier != null && _alreadyCloned.ContainsKey(identifier))
            {
                return(_alreadyCloned[identifier]);
            }

            var resObject = appendToValue ?? _settings.OnCreateInstance(primaryType);

            if (identifier != null)
            {
                _alreadyCloned.Add(identifier, resObject);
            }

            foreach (var property in properties.Values)
            {
                if (!property.CanRead || property.FastDeepClonerIgnore)
                {
                    continue;
                }
                var value = property.GetValue(objectToBeCloned);
                if (value == null)
                {
                    continue;
                }

                if ((property.IsInternalType || value.GetType().IsInternalType()))
                {
                    property.SetValue(resObject, value);
                }
                else
                {
                    if (_settings.CloneLevel == CloneLevel.FirstLevelOnly)
                    {
                        continue;
                    }
                    property.SetValue(resObject, Clone(value));
                }
            }

            return(resObject);
        }
Example #2
0
        private object ReferenceTypeClone(Dictionary <string, IFastDeepClonerProperty> properties, Type primaryType, object objectToBeCloned, object appendToValue = null)
        {
            var resObject = appendToValue ?? _settings.OnCreateInstance(primaryType);
            var fullPath  = primaryType.Name;

            foreach (var property in properties.Values)
            {
                if (!property.CanRead || property.FastDeepClonerIgnore)
                {
                    continue;
                }
                var value = property.GetValue(objectToBeCloned);
                if (value == null)
                {
                    continue;
                }
                var key        = fullPath + property.Name;
                var clonedItem = new ClonedItems()
                {
                    Value = value, Key = key
                };
                if (_alreadyCloned.ContainsKey(clonedItem))
                {
                    property.SetValue(resObject, _alreadyCloned[clonedItem]);
                    continue;
                }

                if (property.IsInternalType || value.GetType().IsInternalType())
                {
                    property.SetValue(resObject, value);
                }
                else
                {
                    if (_settings.CloneLevel == CloneLevel.FirstLevelOnly)
                    {
                        continue;
                    }
                    var tValue = Clone(value);
                    _alreadyCloned.Add(clonedItem, tValue);
                    property.SetValue(resObject, tValue);
                }
            }

            return(resObject);
        }