/// <summary>
 /// The execute match.
 /// </summary>
 /// <param name="mi">
 /// The mi.
 /// </param>
 protected virtual void ExecuteMatch(SmartMatchInfo mi)
 {
     SetValue(mi.TargetProp, mi.Target, GetValue(mi.SourceProp, mi.Source));
 }
        /// <summary>
        /// The execute deep match.
        /// </summary>
        /// <param name="mi">
        /// The mi.
        /// </param>
        /// <exception cref="NotImplementedException">
        /// </exception>
        protected void ExecuteDeepMatch(SmartMatchInfo mi)
        {
            var sourceVal = GetValue(mi.SourceProp, mi.Source);

            if (sourceVal == null)
            {
                return;
            }

            // for value types and string just return the value as is
            if (mi.SourceProp.PropertyType.IsValueType || mi.SourceProp.PropertyType == typeof(string))
            {
                SetValue(mi.TargetProp, mi.Target, sourceVal);
                return;
            }

            int propDepth;
            var propFound = _propertyDict.TryGetValue(mi.SourceProp.Name, out propDepth);

            // handle arrays
            if (mi.SourceProp.PropertyType.IsArray)
            {
                var arr = sourceVal as Array;

                // ReSharper disable once PossibleNullReferenceException
                var arrayClone = arr.Clone() as Array;

                for (var index = 0; index < arr.Length; index++)
                {
                    var arriVal = arr.GetValue(index);
                    if (arriVal.GetType().IsValueType || arriVal is string)
                    {
                        continue;
                    }

                    if (propFound && propDepth > 0)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        arrayClone.SetValue(Activator.CreateInstance(arriVal.GetType()).InjectFrom(new MaxDepthCloneInjector(propDepth), arriVal), index);
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        arrayClone.SetValue(Activator.CreateInstance(arriVal.GetType()).InjectFrom <SmartConventionInjection>(arriVal), index);
                    }
                }

                SetValue(mi.TargetProp, mi.Target, arrayClone);
                return;
            }

            if (mi.SourceProp.PropertyType.IsGenericType)
            {
                // handle IEnumerable<> also ICollection<> IList<> List<>
                if (mi.SourceProp.PropertyType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
                {
                    var genericArgument = mi.TargetProp.PropertyType.GetGenericArguments()[0];

                    var tlist = typeof(List <>).MakeGenericType(genericArgument);

                    var list = Activator.CreateInstance(tlist);

                    if (genericArgument.IsValueType || genericArgument == typeof(string))
                    {
                        var addRange = tlist.GetMethod("AddRange");
                        addRange.Invoke(list, new[] { sourceVal });
                    }
                    else
                    {
                        var addMethod = tlist.GetMethod("Add");

                        // ReSharper disable once PossibleNullReferenceException
                        foreach (var o in sourceVal as IEnumerable)
                        {
                            if (propFound && propDepth > 0)
                            {
                                addMethod.Invoke(list, new[] { Activator.CreateInstance(genericArgument).InjectFrom(new MaxDepthCloneInjector(propDepth), o) });
                            }
                            else
                            {
                                addMethod.Invoke(list, new[] { Activator.CreateInstance(genericArgument).InjectFrom <SmartConventionInjection>(o) });
                            }
                        }
                    }

                    SetValue(mi.TargetProp, mi.Target, list);
                    return;
                }

                throw new NotImplementedException(string.Format("deep clonning for generic type {0} is not implemented", mi.SourceProp.Name));
            }

            // for simple object types create a new instace and apply the clone injection on it
            if (propFound && propDepth > 0)
            {
                SetValue(mi.TargetProp, mi.Target, Activator.CreateInstance(mi.TargetProp.PropertyType).InjectFrom(new MaxDepthCloneInjector(propDepth), sourceVal));
            }
            else
            {
                SetValue(mi.TargetProp, mi.Target, Activator.CreateInstance(mi.TargetProp.PropertyType).InjectFrom <SmartConventionInjection>(sourceVal));
            }
        }
Example #3
0
        /// <summary>
        /// The execute match.
        /// </summary>
        /// <param name="mi">
        /// The mi.
        /// </param>
        protected void ExecuteMatch(SmartMatchInfo mi)
        {
            var sourceValue = GetValue(mi.SourceProp, mi.Source);

            _compositeCollection.Add(new KeyValuePair <string, object>(mi.SourceProp.Name, sourceValue));
        }
        /// <summary>
        /// The execute deep match.
        /// </summary>
        /// <param name="mi">
        /// The mi.
        /// </param>
        /// <exception cref="NotImplementedException">
        /// </exception>
        protected void ExecuteDeepMatch(SmartMatchInfo mi)
        {
            var sourceVal = GetValue(mi.SourceProp, mi.Source);
            if (sourceVal == null)
            {
                return;
            }

            // for value types and string just return the value as is
            if (mi.SourceProp.PropertyType.IsValueType || mi.SourceProp.PropertyType == typeof(string))
            {
                SetValue(mi.TargetProp, mi.Target, sourceVal);
                return;
            }

            // handle arrays
            if (mi.SourceProp.PropertyType.IsArray)
            {
                var arr = sourceVal as Array;

                // ReSharper disable once PossibleNullReferenceException
                var arrayClone = arr.Clone() as Array;

                for (var index = 0; index < arr.Length; index++)
                {
                    var arriVal = arr.GetValue(index);
                    if (arriVal.GetType().IsValueType || arriVal is string)
                    {
                        continue;
                    }

                    if (_maxDepth > 1)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        arrayClone.SetValue(Activator.CreateInstance(arriVal.GetType()).InjectFrom(new MaxDepthCloneInjector(_maxDepth - 1), arriVal), index);
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        arrayClone.SetValue(Activator.CreateInstance(arriVal.GetType()).InjectFrom<SmartConventionInjection>(arriVal), index);
                    }
                }

                SetValue(mi.TargetProp, mi.Target, arrayClone);
                return;
            }

            if (mi.SourceProp.PropertyType.IsGenericType)
            {
                // handle IEnumerable<> also ICollection<> IList<> List<>
                if (mi.SourceProp.PropertyType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
                {
                    var genericArgument = mi.TargetProp.PropertyType.GetGenericArguments()[0];

                    var tlist = typeof(List<>).MakeGenericType(genericArgument);

                    var list = Activator.CreateInstance(tlist);

                    if (genericArgument.IsValueType || genericArgument == typeof(string))
                    {
                        var addRange = tlist.GetMethod("AddRange");
                        addRange.Invoke(list, new[] { sourceVal });
                    }
                    else
                    {
                        var addMethod = tlist.GetMethod("Add");

                        // ReSharper disable once PossibleNullReferenceException
                        foreach (var o in sourceVal as IEnumerable)
                        {
                            if (_maxDepth > 1)
                            {
                                addMethod.Invoke(list, new[] { Activator.CreateInstance(genericArgument).InjectFrom(new MaxDepthCloneInjector(_maxDepth - 1), o) });
                            }
                            else
                            {
                                addMethod.Invoke(list, new[] { Activator.CreateInstance(genericArgument).InjectFrom<SmartConventionInjection>(o) });
                            }
                        }
                    }

                    SetValue(mi.TargetProp, mi.Target, list);
                    return;
                }

                throw new NotImplementedException(string.Format("deep clonning for generic type {0} is not implemented", mi.SourceProp.Name));
            }

            // for simple object types create a new instace and apply the clone injection on it
            if (_maxDepth > 1)
            {
                SetValue(mi.TargetProp, mi.Target, Activator.CreateInstance(mi.TargetProp.PropertyType).InjectFrom(new MaxDepthCloneInjector(_maxDepth - 1), sourceVal));
            }
            else
            {
                SetValue(mi.TargetProp, mi.Target, Activator.CreateInstance(mi.TargetProp.PropertyType).InjectFrom<SmartConventionInjection>(sourceVal));
            }
        }
 /// <summary>
 /// The execute match.
 /// </summary>
 /// <param name="mi">
 /// The mi.
 /// </param>
 protected virtual void ExecuteMatch(SmartMatchInfo mi)
 {
     SetValue(mi.TargetProp, mi.Target, GetValue(mi.SourceProp, mi.Source));
 }