Ejemplo n.º 1
0
        /// <summary>
        /// 访问表达式树后自动删掉访问的成员痕迹
        /// </summary>
        /// <param name="visit">访问委托</param>
        protected void VisitWithoutRemark(Action <object> visit)
        {
            int visitedQty = _visitedStack.Count;

            visit(null);
            if (_visitedStack.Count != visitedQty)
            {
                _visitedStack.Remove(_visitedStack.Count - visitedQty);
            }
        }
        /// <summary>
        ///  将调度到此类中更专用的访问方法之一的表达式。
        /// </summary>
        /// <param name="node">方法节点</param>
        /// <param name="router">方法路由</param>
        /// <returns></returns>
        public Expression Visit(Expression node, MethodCallType router)
        {
            int        visitedQty = _visitedStack.Count;
            Expression newNode    = null;

            if (router == MethodCallType.Coalesce)
            {
                newNode = this.VisitCoalesce((BinaryExpression)node);
            }
            else if (router == MethodCallType.EqualNull)
            {
                newNode = this.VisitEqualNull((BinaryExpression)node);
            }
            else if (router == MethodCallType.MethodCall)
            {
                newNode = this.VisitMethodCall((MethodCallExpression)node);
            }
            else if (router == MethodCallType.BinaryCall)
            {
                newNode = this.VisitMethodCall((BinaryExpression)node);
            }
            else if (router == MethodCallType.MemberMember)
            {
                newNode = this.VisitMemberMember((MemberExpression)node);
            }
            else if (router == MethodCallType.Unary)
            {
                newNode = this.VisitUnary((UnaryExpression)node);
            }

            // 自身已构成布尔表达式的则需要删除它本身所产生的访问链
            if (_visitedStack.Count != visitedQty)
            {
                bool b = router != MethodCallType.Unary;
                if (b && router == MethodCallType.MethodCall)
                {
                    var m = (MethodCallExpression)node;
                    b = _removeVisitedMethods.Contains(m.Method.Name);
                }

                if (b)
                {
                    _visitedStack.Remove(_visitedStack.Count - visitedQty);
                }
            }
            return(newNode);
        }