Exemple #1
0
        public void Map <TSource, TTarget>(TSource source, TTarget target, IService service, params Expression <Func <TTarget, object> >[] collectionsOwningItems)
            where TSource : class
            where TTarget : class
        {
            Requires <ArgumentNullException> .IsNotNull(source);

            Requires <ArgumentNullException> .IsNotNull(target);

            Requires <ArgumentNullException> .IsNotNull(collectionsOwningItems);

            foreach (var collectionsOwningItem in collectionsOwningItems)
            {
                var propertyName = ExpressionHelper.GetExpressionText(collectionsOwningItem);
                var propertyType = target.GetType().GetProperty(propertyName).PropertyType;
                Requires <InvalidOperationException> .IsTrue(propertyType.IsGenericList(), string.Format("Property '{0}' is not a generic List but '{1}'", propertyName, propertyType));
            }

            Type sourceType = typeof(TSource);
            Type targetType = typeof(TTarget);

            EasyMapper.Context.Set(typeof(TSource), typeof(TTarget));
            var map = Maps[sourceType, targetType];

            PropertyPath sourcePropertyPath = null;

            foreach (var targetPropertyPath in map.Keys)
            {
                sourcePropertyPath = map.Get(targetPropertyPath);
                Requires <InvalidOperationException> .IsNotNull(sourcePropertyPath);

                if (!sourcePropertyPath.CanConvert())
                {
                    continue;
                }
                sourcePropertyPath.SetValue(target, source, targetPropertyPath, this, service, collectionsOwningItems);
            }
        }