Пример #1
0
        /// <summary>
        /// Try to convert a value into an EDM primitive type, if template parsing enabled, the <paramref name="valueText"/> matching
        /// template would be converted into corresponding UriTemplateExpression.
        /// </summary>
        /// <param name="typeReference">the type to convert to (primitive or enum type)</param>
        /// <param name="valueText">the value to convert</param>
        /// <param name="convertedValue">The converted value, if conversion succeeded.</param>
        /// <returns>true if the conversion was successful.</returns>
        private bool TryConvertValue(IEdmTypeReference typeReference, string valueText, out object convertedValue)
        {
            if (typeReference.IsEnum())
            {
                QueryNode enumNode = null;
                if (EnumBinder.TryBindIdentifier(valueText, typeReference.AsEnum(), null, out enumNode))
                {
                    convertedValue = enumNode;
                    return(true);
                }

                convertedValue = null;
                return(false);
            }

            IEdmPrimitiveTypeReference primitiveType = typeReference.AsPrimitive();
            UriTemplateExpression      expression;

            if (this.enableUriTemplateParsing && UriTemplateParser.TryParseLiteral(valueText, primitiveType, out expression))
            {
                convertedValue = expression;
                return(true);
            }

            Type          primitiveClrType = EdmLibraryExtensions.GetPrimitiveClrType((IEdmPrimitiveType)primitiveType.Definition, primitiveType.IsNullable);
            LiteralParser literalParser    = LiteralParser.ForKeys(this.keysAsSegment);

            return(literalParser.TryParseLiteral(primitiveClrType, valueText, out convertedValue));
        }
        /// <summary>
        /// Try to convert a value into an EDM primitive type, if template parsing enabled, the <paramref name="valueText"/> matching
        /// template would be converted into corresponding UriTemplateExpression.
        /// </summary>
        /// <param name="primitiveType">the type to convert to</param>
        /// <param name="valueText">the value to convert</param>
        /// <param name="convertedValue">The converted value, if conversion succeeded.</param>
        /// <returns>true if the conversion was successful.</returns>
        private bool TryConvertValue(IEdmPrimitiveTypeReference primitiveType, string valueText, out object convertedValue)
        {
            UriTemplateExpression expression;

            if (this.enableUriTemplateParsing && UriTemplateParser.TryParseLiteral(valueText, primitiveType, out expression))
            {
                convertedValue = expression;
                return(true);
            }

            Type          primitiveClrType = EdmLibraryExtensions.GetPrimitiveClrType((IEdmPrimitiveType)primitiveType.Definition, primitiveType.IsNullable);
            LiteralParser literalParser    = LiteralParser.ForKeys(this.keysAsSegments);

            return(literalParser.TryParseLiteral(primitiveClrType, valueText, out convertedValue));
        }