Example #1
0
        /// <summary>
        /// Gets the query operator <see cref="MethodCallExpression"/> represented by <paramref name="expression"/>. If <paramref name="expression"/>
        /// is already a <see cref="MethodCallExpression"/>, that is the assumed query operator. If <paramref name="expression"/> is a
        /// <see cref="MemberExpression"/> and the member's getter is registered with <see cref="NodeTypeProvider"/>, a corresponding
        /// <see cref="MethodCallExpression"/> is constructed and returned. Otherwise, <see langword="null" /> is returned.
        /// </summary>
        /// <param name="expression">The expression to get a query operator expression for.</param>
        /// <returns>A <see cref="MethodCallExpression"/> to be parsed as a query operator, or <see langword="null"/> if the expression does not represent
        /// a query operator.</returns>
        public MethodCallExpression GetQueryOperatorExpression(Expression expression)
        {
            var methodCallExpression = expression as MethodCallExpression;

            if (methodCallExpression != null)
            {
                return(methodCallExpression);
            }

            var memberExpression = expression as MemberExpression;

            if (memberExpression != null)
            {
                var propertyInfo = memberExpression.Member as PropertyInfo;
                if (propertyInfo == null)
                {
                    return(null);
                }

#if NETFX_CORE
                var getterMethod = propertyInfo.GetMethod;
#else
                var getterMethod = propertyInfo.GetGetMethod();
#endif
                if (getterMethod == null || !_nodeTypeProvider.IsRegistered(getterMethod))
                {
                    return(null);
                }

                return(Expression.Call(memberExpression.Expression, getterMethod));
            }

            var unaryExpression = expression as UnaryExpression;
            if (unaryExpression != null)
            {
                if (unaryExpression.NodeType == ExpressionType.ArrayLength && _nodeTypeProvider.IsRegistered(s_getArrayLengthMethod))
                {
                    return(Expression.Call(unaryExpression.Operand, s_getArrayLengthMethod));
                }
            }

            return(null);
        }
Example #2
0
        public bool IsRegistered(MethodInfo method)
        {
            // Avoid Relinq turning IDictionary.Contains into ContainsResultOperator.  We do our own processing for that method.
            if (method.DeclaringType == typeof(IDictionary) && method.Name == "Contains")
            {
                return(false);
            }

            return(defaultNodeTypeProvider.IsRegistered(method));
        }
Example #3
0
        public override Expression Visit(Expression expression)
        {
            var potentialQueryOperatorExpression = _expressionTreeParser.GetQueryOperatorExpression(expression);

            if (potentialQueryOperatorExpression != null &&
                _nodeTypeProvider.IsRegistered(potentialQueryOperatorExpression.Method))
            {
                return(CreateSubQueryNode(potentialQueryOperatorExpression));
            }
            return(base.Visit(expression));
        }
Example #4
0
 public bool IsRegistered(MethodInfo method)
 {
     return(_inner.IsRegistered(method));
 }
 public bool IsRegistered(MethodInfo method) => _nodeTypeProvider.IsRegistered(method);
Example #6
0
        protected override Expression VisitMethodCallExpression(MethodCallExpression expression)
        {
            if (!expression.Method.IsStatic && expression.Method.IsSpecialName && expression.Method.Name == "get_Item" && expression.Arguments.Count == 1)
            {
                var arg = expression.Arguments[0] as ConstantExpression;
                if (arg != null && arg.Type == typeof(string))
                {
                    if (_selectMode && _parts.Count == 0 && !_convertedToSimple)
                    {
                        _parts.Add("*");
                    }
                    _parts.Add((string)arg.Value);
                    VisitExpression(expression.Object);
                    return(expression);
                }
            }
            else if (_nodeTypeProvider.IsRegistered(expression.Method))
            {
                Expression         source;
                IList <Expression> args;
                if (expression.Object != null)
                {
                    source = expression.Object;
                    args   = expression.Arguments;
                }
                else
                {
                    source = expression.Arguments[0];
                    args   = expression.Arguments.Skip(1).ToList();
                }

                var sourcePaths = ExtractPaths(source, _nodeTypeProvider, _selectMode, _includeProtocolProps, _namingScheme);
                var argPaths    = args.SelectMany(arg => ExtractPaths(arg, _nodeTypeProvider, _selectMode, _includeProtocolProps, _namingScheme)).ToList();

                foreach (var sourcePath in sourcePaths)
                {
                    var isPassthrough = expression.Method.Name != "Select" &&
                                        expression.Method.Name != "SelectMany" &&
                                        (expression.Type.IsAssignableFrom(source.Type) ||
                                         typeof(IEnumerable <>).MakeGenericType(expression.Type).IsAssignableFrom(source.Type));
                    if (isPassthrough)
                    {
                        _paths.Add(sourcePath.ToList());
                    }
                    if (sourcePath[0] == "*")
                    {
                        sourcePath.RemoveAt(0);
                    }
                    if (!isPassthrough && argPaths.Count == 0)
                    {
                        _paths.Add(sourcePath.ToList());
                    }
                    foreach (var argPath in argPaths)
                    {
                        _paths.Add(argPath.Concat(sourcePath).ToList());
                    }
                }

                return(expression);
            }

            return(base.VisitMethodCallExpression(expression));
        }
 public bool IsRegistered(MethodInfo method)
 {
     return(defaultNodeTypeProvider.IsRegistered(method));
 }