Example #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));
        }