Exemplo n.º 1
0
        /// <summary>
        /// ConstantExpression visit method
        /// </summary>
        /// <param name="c">The ConstantExpression expression to visit</param>
        /// <returns>The visited ConstantExpression expression </returns>
        internal override Expression VisitConstant(ConstantExpression c)
        {
            if (c.Value == null)
            {
                this.builder.Append(UriHelper.NULL);
                return(c);
            }

            // DEVNOTE:
            // Rather than forcing every other codepath to have the 'Try...' pattern for formatting,
            // we catch the InvalidOperationException here to change the exception type.
            // This is exceedingly rare, and not a scenario where performance is meaningful, so the
            // reduced complexity in all other call sites is worth the extra logic here.
            string result;

            try
            {
                result = LiteralFormatter.ForConstants.Format(c.Value);
            }
            catch (InvalidOperationException)
            {
                if (this.cantTranslateExpression)
                {
                    // there's already a problem in the parents.
                    // we should just return here, because caller somewhere up the stack will throw a better exception
                    return(c);
                }

                throw new NotSupportedException(Strings.ALinq_CouldNotConvert(c.Value));
            }

            Debug.Assert(result != null, "result != null");
            this.builder.Append(result);
            return(c);
        }
        internal override Expression VisitConstant(ConstantExpression c)
        {
            string result = null;

            if (c.Value == null)
            {
                this.builder.Append(UriHelper.NULL);
                return(c);
            }
            else if (!ClientConvert.TryKeyPrimitiveToString(c.Value, out result))
            {
                throw new InvalidOperationException(Strings.ALinq_CouldNotConvert(c.Value));
            }

            Debug.Assert(result != null, "result != null");
            this.builder.Append(System.Uri.EscapeDataString(result));
            return(c);
        }