Gathers information about properties of an object used in an expression.
Inheritance: System.Linq.Expressions.ExpressionVisitor
Esempio n. 1
0
        /// <summary>
        /// The entry-point of <see cref="PageSelectVisitor"/>.
        /// </summary>
        public static IEnumerable <PropQueryParameters> Process <TSource, TResult>(
            Expression <Func <TSource, TResult> > expression, out Func <PageData, TResult> processedExpression)
        {
            var visitor = new PageSelectVisitor(expression.Parameters.Single());
            var body    = visitor.Visit(expression.Body);

            var gatherer = new UsedPropertiesGatherer();

            gatherer.Gather(body, visitor.m_pageDataGetInfoCall);

            var parameters = visitor.m_parameters;

            if (!gatherer.UsedDirectly && gatherer.UsedProperties.Any(p => !NonInfoProperties.Contains(p)))
            {
                var propQueryParameters = new PropQueryParameters("info").WithProperties(gatherer.UsedProperties);

                var tokens = gatherer.UsedProperties
                             .Where(p => p.EndsWith("token"))
                             .Select(p => p.Substring(0, p.Length - "token".Length))
                             .ToArray();

                if (tokens.Any())
                {
                    propQueryParameters = propQueryParameters.AddSingleValue("token", tokens.ToQueryString());
                }

                parameters["info"] = propQueryParameters;
            }

            processedExpression =
                Expression.Lambda <Func <PageData, TResult> >(body, visitor.m_pageDataParameter).Compile();

            return(parameters.Values);
        }
        /// <summary>
        /// Parses a <c>select</c> expression.
        /// </summary>
        /// <param name="expression">Expression to parse.</param>
        /// <param name="previousParameters">Previous parameters, whose values should be included in the result.</param>
        public static QueryParameters <TSource, TResult> ParseSelect <TSource, TResult>(
            Expression <Func <TSource, TResult> > expression, QueryParameters <TSource, TSource> previousParameters)
        {
            var parameter = expression.Parameters.Single();

            var gatherer = new UsedPropertiesGatherer();

            gatherer.Gather(expression.Body, parameter);

            var usedProperties = gatherer.UsedDirectly ? null : gatherer.UsedProperties.Select(p => p.ToLowerInvariant());

            return(previousParameters.WithSelect(usedProperties, expression.Compile()));
        }