Пример #1
0
        /// <summary>
        /// Visits the instance function for Trim specific handling.
        /// </summary>
        private static void VisitParameterizedTrimFunc(MethodCallExpression expression,
                                                       CacheQueryExpressionVisitor visitor, string func)
        {
            visitor.ResultBuilder.Append(func).Append("(");

            visitor.Visit(expression.Object);

            var arg = expression.Arguments[0];

            if (arg != null)
            {
                visitor.ResultBuilder.Append(", ");

                if (arg.NodeType == ExpressionType.Constant)
                {
                    var constant = (ConstantExpression)arg;

                    if (constant.Value is char)
                    {
                        visitor.AppendParameter((char)constant.Value);
                    }
                    else
                    {
                        var args = constant.Value as IEnumerable <char>;

                        if (args == null)
                        {
                            throw new NotSupportedException("String.Trim function only supports IEnumerable<char>");
                        }

                        var enumeratedArgs = args.ToArray();

                        if (enumeratedArgs.Length != 1)
                        {
                            throw new NotSupportedException("String.Trim function only supports a single argument: " +
                                                            expression);
                        }

                        visitor.AppendParameter(enumeratedArgs[0]);
                    }
                }
                else
                {
                    visitor.Visit(arg);
                }
            }

            visitor.ResultBuilder.Append(")");
        }
Пример #2
0
        /// <summary>
        /// Visits the constant call expression.
        /// </summary>
        public static bool VisitConstantCall(ConstantExpression expression, CacheQueryExpressionVisitor visitor)
        {
            if (expression.Type != typeof(RegexOptions))
            {
                return(false);
            }

            var regexOptions = expression.Value as RegexOptions? ?? RegexOptions.None;
            var result       = string.Empty;

            foreach (var option in RegexOptionFlags)
            {
                if (regexOptions.HasFlag(option.Key))
                {
                    result       += option.Value;
                    regexOptions &= ~option.Key;
                }
            }

            if (regexOptions != RegexOptions.None)
            {
                throw new NotSupportedException(string.Format("RegexOptions.{0} is not supported", regexOptions));
            }

            visitor.AppendParameter(result);

            return(true);
        }