Beispiel #1
0
        protected override Expression VisitMethodCall(MethodCallExpression m)
        {
            if (m.Method.DeclaringType == typeof(String) && m.Method.Name == "StartsWith")
            {
                if (ExpressionTreeHelpers.IsSpecificMemberExpression(m.Object, typeof(Place), "Name") ||
                    ExpressionTreeHelpers.IsSpecificMemberExpression(m.Object, typeof(Place), "State"))
                {
                    locations.Add(ExpressionTreeHelpers.GetValueFromExpression(m.Arguments[0]));
                    return(m);
                }
            }
            else if (m.Method.Name == "Contains")
            {
                Expression valuesExpression = null;

                if (m.Method.DeclaringType == typeof(Enumerable))
                {
                    if (ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[1], typeof(Place), "Name") ||
                        ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[1], typeof(Place), "State"))
                    {
                        valuesExpression = m.Arguments[0];
                    }
                }
                else if (m.Method.DeclaringType == typeof(List <string>))
                {
                    if (ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[0], typeof(Place), "Name") ||
                        ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[0], typeof(Place), "State"))
                    {
                        valuesExpression = m.Object;
                    }
                }

                if (valuesExpression == null || valuesExpression.NodeType != ExpressionType.Constant)
                {
                    throw new InvalidQueryException("Could not find the location values.");
                }

                ConstantExpression ce = (ConstantExpression)valuesExpression;

                IEnumerable <string> placeStrings = (IEnumerable <string>)ce.Value;
                // Add each string in the collection to the list of locations to obtain data about.
                foreach (string place in placeStrings)
                {
                    locations.Add(place);
                }

                return(m);
            }

            return(base.VisitMethodCall(m));
        }
        protected override Expression VisitMethodCall(MethodCallExpression m)
        {
            if (m.Method.DeclaringType == typeof(String) && m.Method.Name == "StartsWith")
            {
                if (ExpressionTreeHelpers.IsSpecificMemberExpression(m.Object, typeof(Place), "Name") ||
                    ExpressionTreeHelpers.IsSpecificMemberExpression(m.Object, typeof(Place), "State"))
                {
                    locations.Add(ExpressionTreeHelpers.GetValueFromExpression(m.Arguments[0]));
                    return(m);
                }
            }
            else if (m.Method.Name == "Contains")
            {
                Expression valuesExpression = null;

                if (m.Method.DeclaringType == typeof(Enumerable))
                {
                    if (ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[1], typeof(Place), "Name") ||
                        ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[1], typeof(Place), "State"))
                    {
                        valuesExpression = m.Arguments[0];
                    }
                }
                else if (m.Method.DeclaringType == typeof(List <string>))
                {
                    if (ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[0], typeof(Place), "Name") ||
                        ExpressionTreeHelpers.IsSpecificMemberExpression(m.Arguments[0], typeof(Place), "State"))
                    {
                        valuesExpression = m.Object;
                    }
                }

                if (valuesExpression == null || valuesExpression.NodeType != ExpressionType.Constant)
                {
                    throw new InvalidQueryException("Could not find the location values.");
                }

                ConstantExpression ce = (ConstantExpression)valuesExpression;

                IEnumerable <string> placeStrings = (IEnumerable <string>)ce.Value;
                // Добавить каждую строку коллекции в список расположений, чтобы получить соответствующие данные.
                foreach (string place in placeStrings)
                {
                    locations.Add(place);
                }

                return(m);
            }

            return(base.VisitMethodCall(m));
        }
        internal static bool IsMemberEqualsValueExpression(Expression exp, Type declaringType, string memberName)
        {
            if (exp.NodeType != ExpressionType.Equal)
            {
                return(false);
            }

            BinaryExpression be = (BinaryExpression)exp;

            if (ExpressionTreeHelpers.IsSpecificMemberExpression(be.Left, declaringType, memberName) &&
                ExpressionTreeHelpers.IsSpecificMemberExpression(be.Right, declaringType, memberName))
            {
                throw new InvalidOperationException("Cannot have 'member' == 'member' in an expression!");
            }

            return(ExpressionTreeHelpers.IsSpecificMemberExpression(be.Left, declaringType, memberName) ||
                   ExpressionTreeHelpers.IsSpecificMemberExpression(be.Right, declaringType, memberName));
        }