Exemplo n.º 1
0
        /// <summary>
        /// Visit the children of <see cref="T:System.Linq.Expressions.MemberExpression" />.
        /// </summary>
        /// <param name="node">Expression to visit.</param>
        /// <returns>
        /// Altered expression, if it or any subexpression was modified; otherwise, returns the original expression.
        /// </returns>
        protected override Expression VisitMember(MemberExpression node)
        {
            var expr = Visit(node.Expression);

            if (expr != null && expr.Type != node.Type)
            {
                if (mapper == null)
                {
                    mapper = Mapper.GetMapper(node.Member.DeclaringType, destinationType);
                }
                Expression expDest = null;
                // We consider that the primitive class is the simple property(not a sub's object).
                if (!expr.Type.IsValueType && expr.Type != typeof(string) &&
                    expr.NodeType != ExpressionType.Parameter && expr.NodeType != ExpressionType.Constant)
                {
                    var subExp = Mapper.GetMapper(node.Member.DeclaringType, expr.Type);
                    expDest = subExp.GetLambdaDest(node.Member.Name);
                    return(AnalyseDestExpression(expr, expDest));
                }
                else
                {
                    expDest = mapper.GetLambdaDest(node.Member.Name);
                    if (expDest != null)
                    {
                        return(AnalyseDestExpression(expr, expDest));
                    }
                }
            }
            return(base.VisitMember(node));
        }
        /// <summary>
        /// 访问子表达式树
        /// </summary>
        /// <param name="node">访问表达式树</param>
        /// <returns>
        /// 改变表达式,如果它或它的任何子表达式被修改; 否则,返回原始表达式。
        /// </returns>
        protected override Expression VisitMember(MemberExpression node)
        {
            var expr = Visit(node.Expression);

            if (expr != null && expr.Type != node.Type)
            {
                if (_mapper == null)
                {
                    _mapper = ExpressionMapper.GetMapper(node.Member.DeclaringType, _destinationType);
                }
                Expression expDest;
                // 认为原始类是简单属性(不是子对象)。
                if (!expr.Type.IsValueType && expr.Type != typeof(string) && expr.NodeType != ExpressionType.Parameter && expr.NodeType != ExpressionType.Constant)
                {
                    var subExp = ExpressionMapper.GetMapper(node.Member.DeclaringType, expr.Type);
                    expDest = subExp.GetLambdaDest(node.Member.Name);
                    return(AnalyseDestExpression(expr, expDest));
                }
                expDest = _mapper.GetLambdaDest(node.Member.Name);
                if (expDest != null)
                {
                    return(AnalyseDestExpression(expr, expDest));
                }
            }
            return(base.VisitMember(node));
        }
 /// <summary>
 /// Visit the children of <see cref="T:System.Linq.Expressions.MemberExpression" />.
 /// </summary>
 /// <param name="node">Expression to visit.</param>
 /// <returns>
 /// Altered expression, if it or any subexpression was modified; otherwise, returns the original expression.
 /// </returns>
 protected override Expression VisitMember(MemberExpression node)
 {
     var expr = Visit(node.Expression);
     if (expr != null && expr.Type != node.Type)
     {
         if (mapper == null)
         {
             mapper = Mapper.GetMapper(node.Member.DeclaringType, destinationType);
         }
         Expression expDest = null;
         // We consider that the primitive class is the simple property(not a sub's object).
         if (!expr.Type.IsValueType && expr.Type != typeof(string) && 
             expr.NodeType != ExpressionType.Parameter && expr.NodeType != ExpressionType.Constant)
         {
             var subExp = Mapper.GetMapper(node.Member.DeclaringType, expr.Type);
             expDest = subExp.GetLambdaDest(node.Member.Name);
             return AnalyseDestExpression(expr, expDest);
         }
         else
         {
             expDest = mapper.GetLambdaDest(node.Member.Name);
             if (expDest != null)
             {
                 return AnalyseDestExpression(expr, expDest);
             }
         }
     }
         return base.VisitMember(node); 
 }
Exemplo n.º 4
0
        public void NewMapperConfigurationBase_SetProperties()
        {
            MapperConfigurationBase actual = null;

            actual = new MapperConfiguration <ClassSource, ClassDest>("sourceTest");
            Assert.IsNotNull(actual.MemberToMapForNew);
            Assert.AreEqual(actual.TargetType, typeof(ClassDest));
            Assert.AreEqual(actual.SourceType, typeof(ClassSource));
        }
Exemplo n.º 5
0
        internal static MapperConfigurationBase GetMapper(Type tSource, Type tDest, string name = null)
        {
            MapperConfigurationBase mapConfig = MapperConfigurationCollectionContainer.Instance.Find(tSource, tDest, name);

            if (mapConfig == null)
            {
                throw new NoFoundMapperException(tSource, tDest);
            }

            return(mapConfig);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 创建mapper对象
        /// </summary>
        /// <typeparam name="TSource">源类型</typeparam>
        /// <typeparam name="TDest">目标类型</typeparam>
        /// <returns></returns>
        public static MapperConfiguration <TSource, TDest> CreateMap <TSource, TDest>(string name = null) where TSource : class where TDest : class
        {
            MapperConfigurationBase map = MapperConfigurationCollectionContainer.Instance.Find(typeof(TSource), typeof(TDest), name);

            if (map == null)
            {
                string finalName = string.IsNullOrEmpty(name) ? "s" + MapperConfigurationCollectionContainer.Instance.Count.ToString() : name;
                map = new MapperConfiguration <TSource, TDest>(finalName);
                MapperConfigurationCollectionContainer.Instance.Add(map);
            }
            return(map as MapperConfiguration <TSource, TDest>);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates a mapper.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TTarget">The type of the dest.</typeparam>
        /// <returns></returns>
        public static MapperConfiguration <TSource, TTarget> CreateMap <TSource, TTarget>(string name = null)
            where TSource : class
            where TTarget : class
        {
            // We do not use the method because it GetMapper throw an exception if not found
            MapperConfigurationBase map = MapperConfigurationCollectionContainer.Instance.Find(typeof(TSource), typeof(TTarget), name);

            if (map == null)
            {
                string finalName = string.IsNullOrEmpty(name) ? "s" + MapperConfigurationCollectionContainer.Instance.Count.ToString() : name;
                map = new MapperConfiguration <TSource, TTarget>(finalName);
                MapperConfigurationCollectionContainer.Instance.Add(map);
            }
            return(map as MapperConfiguration <TSource, TTarget>);
        }