예제 #1
0
        /// <summary>
        /// Converts a string to a primitive value.
        /// </summary>
        /// <param name="text">The string text to convert.</param>
        /// <param name="targetTypeReference">Type to convert the string to.</param>
        /// <returns>The value converted to the target type.</returns>
        /// <remarks>This method does not convert null value.</remarks>
        internal static object ConvertStringToPrimitive(string text, IEdmPrimitiveTypeReference targetTypeReference)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(text != null, "text != null");
            Debug.Assert(targetTypeReference != null, "targetTypeReference != null");

            try
            {
                EdmPrimitiveTypeKind primitiveKind = targetTypeReference.PrimitiveKind();

                switch (primitiveKind)
                {
                case EdmPrimitiveTypeKind.Binary:
                    return(Convert.FromBase64String(text));

                case EdmPrimitiveTypeKind.Boolean:
                    return(ConvertXmlBooleanValue(text));

                case EdmPrimitiveTypeKind.Byte:
                    return(XmlConvert.ToByte(text));

                case EdmPrimitiveTypeKind.DateTime:
                    return(PlatformHelper.ConvertStringToDateTime(text));

                case EdmPrimitiveTypeKind.DateTimeOffset:
                    return(XmlConvert.ToDateTimeOffset(text));

                case EdmPrimitiveTypeKind.Decimal:
                    return(XmlConvert.ToDecimal(text));

                case EdmPrimitiveTypeKind.Double:
                    return(XmlConvert.ToDouble(text));

                case EdmPrimitiveTypeKind.Guid:
                    return(new Guid(text));

                case EdmPrimitiveTypeKind.Int16:
                    return(XmlConvert.ToInt16(text));

                case EdmPrimitiveTypeKind.Int32:
                    return(XmlConvert.ToInt32(text));

                case EdmPrimitiveTypeKind.Int64:
                    return(XmlConvert.ToInt64(text));

                case EdmPrimitiveTypeKind.SByte:
                    return(XmlConvert.ToSByte(text));

                case EdmPrimitiveTypeKind.Single:
                    return(XmlConvert.ToSingle(text));

                case EdmPrimitiveTypeKind.String:
                    return(text);

                case EdmPrimitiveTypeKind.Time:
                    return(XmlConvert.ToTimeSpan(text));

                case EdmPrimitiveTypeKind.Stream:
                case EdmPrimitiveTypeKind.None:
                case EdmPrimitiveTypeKind.Geography:
                case EdmPrimitiveTypeKind.GeographyCollection:
                case EdmPrimitiveTypeKind.GeographyPoint:
                case EdmPrimitiveTypeKind.GeographyLineString:
                case EdmPrimitiveTypeKind.GeographyPolygon:
                case EdmPrimitiveTypeKind.GeometryCollection:
                case EdmPrimitiveTypeKind.GeographyMultiPolygon:
                case EdmPrimitiveTypeKind.GeographyMultiLineString:
                case EdmPrimitiveTypeKind.GeographyMultiPoint:
                case EdmPrimitiveTypeKind.Geometry:
                case EdmPrimitiveTypeKind.GeometryPoint:
                case EdmPrimitiveTypeKind.GeometryLineString:
                case EdmPrimitiveTypeKind.GeometryPolygon:
                case EdmPrimitiveTypeKind.GeometryMultiPolygon:
                case EdmPrimitiveTypeKind.GeometryMultiLineString:
                case EdmPrimitiveTypeKind.GeometryMultiPoint:
                default:
                    // Note that Astoria supports XElement and Binary as well, but they are serialized as string or byte[]
                    // and the metadata will actually talk about string and byte[] as well. Astoria will perform the conversion if necessary.
                    throw new ODataException(Strings.General_InternalError(InternalErrorCodes.AtomValueUtils_ConvertStringToPrimitive));
                }
            }
            catch (Exception e)
            {
                if (!ExceptionUtils.IsCatchableExceptionType(e))
                {
                    throw;
                }

                throw ReaderValidationUtils.GetPrimitiveTypeConversionException(targetTypeReference, e);
            }
        }
예제 #2
0
        /// <summary>Converts the specified value to a serializable string in ATOM format.</summary>
        /// <param name="value">Non-null value to convert.</param>
        /// <param name="result">The specified value converted to an ATOM string.</param>
        /// <returns>boolean value indicating conversion successful conversion</returns>
        internal static bool TryConvertPrimitiveToString(object value, out string result)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(value != null, "value != null");
            result = null;

            TypeCode typeCode = PlatformHelper.GetTypeCode(value.GetType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                result = ODataAtomConvert.ToString((bool)value);
                break;

            case TypeCode.Byte:
                result = ODataAtomConvert.ToString((byte)value);
                break;

            case TypeCode.DateTime:
                result = ODataAtomConvert.ToString((DateTime)value);
                break;

            case TypeCode.Decimal:
                result = ODataAtomConvert.ToString((decimal)value);
                break;

            case TypeCode.Double:
                result = ODataAtomConvert.ToString((double)value);
                break;

            case TypeCode.Int16:
                result = ODataAtomConvert.ToString((Int16)value);
                break;

            case TypeCode.Int32:
                result = ODataAtomConvert.ToString((Int32)value);
                break;

            case TypeCode.Int64:
                result = ODataAtomConvert.ToString((Int64)value);
                break;

            case TypeCode.SByte:
                result = ODataAtomConvert.ToString((SByte)value);
                break;

            case TypeCode.String:
                result = (string)value;
                break;

            case TypeCode.Single:
                result = ODataAtomConvert.ToString((Single)value);
                break;

            default:
                byte[] bytes = value as byte[];
                if (bytes != null)
                {
                    result = ODataAtomConvert.ToString(bytes);
                    break;
                }

                if (value is DateTimeOffset)
                {
                    result = ODataAtomConvert.ToString((DateTimeOffset)value);
                    break;
                }

                if (value is Guid)
                {
                    result = ODataAtomConvert.ToString((Guid)value);
                    break;
                }

                if (value is TimeSpan)
                {
                    // Edm.Time
                    result = ODataAtomConvert.ToString((TimeSpan)value);
                    break;
                }

                return(false);
            }

            Debug.Assert(result != null, "result != null");
            return(true);
        }