Beispiel #1
0
        protected override Expression VisitMethodCall(MethodCallExpression methodCall)
        {
            string method = methodCall.Method.Name;

            switch (method)
            {
            case "Contains":
            {
                Expression o       = methodCall.Object;
                string     pattern = methodCall.GetArgumentValue <string>(0);

                HandleRegexMethodCallExpression(o, pattern, false);

                return(methodCall);
            }

            case "StartsWith":
            {
                object[] args = new object[]
                {
                    true,
                    StringComparison.CurrentCultureIgnoreCase,
                    StringComparison.InvariantCultureIgnoreCase
                };

                Expression o          = methodCall.Object;
                string     pattern    = "^" + methodCall.GetArgumentValue <string>(0);
                bool       ignoreCase = methodCall.HasArgumentValueFromAlternatives(1, args);

                HandleRegexMethodCallExpression(o, pattern, ignoreCase);

                return(methodCall);
            }

            case "EndsWith":
            {
                object[] args = new object[]
                {
                    true,
                    StringComparison.CurrentCultureIgnoreCase,
                    StringComparison.InvariantCultureIgnoreCase
                };

                Expression o          = methodCall.Object;
                string     pattern    = methodCall.GetArgumentValue <string>(0) + "$";
                bool       ignoreCase = methodCall.HasArgumentValueFromAlternatives(1, args);

                HandleRegexMethodCallExpression(o, pattern, ignoreCase);

                return(methodCall);
            }

            case "IsMatch":
            {
                if (methodCall.Method.DeclaringType == typeof(Regex))
                {
                    Expression   o       = methodCall.Arguments[0];
                    string       pattern = methodCall.GetArgumentValue <string>(1) + "$";
                    RegexOptions options = methodCall.GetArgumentValue(2, RegexOptions.None);

                    HandleRegexMethodCallExpression(o, pattern, options.HasFlag(RegexOptions.IgnoreCase));

                    return(methodCall);
                }

                break;
            }
            }

            throw new NotSupportedException(methodCall.Method.ToString());
        }