Ejemplo n.º 1
0
        internal static bool TryXmlPrimitiveToString(object value, out string result)
        {
            Type nullableType = value.GetType();

            nullableType = Nullable.GetUnderlyingType(nullableType) ?? nullableType;
            if (typeof(string) == nullableType)
            {
                result = (string)value;
            }
            else if (typeof(bool) == nullableType)
            {
                result = XmlConvert.ToString((bool)value);
            }
            else if (typeof(byte) == nullableType)
            {
                result = XmlConvert.ToString((byte)value);
            }
            else if (typeof(DateTime) == nullableType)
            {
                result = System.Data.Services.Client.PlatformHelper.ConvertDateTimeToString((DateTime)value);
            }
            else if (typeof(decimal) == nullableType)
            {
                result = XmlConvert.ToString((decimal)value);
            }
            else if (typeof(double) == nullableType)
            {
                result = XmlConvert.ToString((double)value);
            }
            else if (typeof(Guid) == nullableType)
            {
                result = value.ToString();
            }
            else if (typeof(short) == nullableType)
            {
                result = XmlConvert.ToString((short)value);
            }
            else if (typeof(int) == nullableType)
            {
                result = XmlConvert.ToString((int)value);
            }
            else if (typeof(long) == nullableType)
            {
                result = XmlConvert.ToString((long)value);
            }
            else if (typeof(sbyte) == nullableType)
            {
                result = XmlConvert.ToString((sbyte)value);
            }
            else if (typeof(float) == nullableType)
            {
                result = XmlConvert.ToString((float)value);
            }
            else if (typeof(byte[]) == nullableType)
            {
                byte[] inArray = (byte[])value;
                result = Convert.ToBase64String(inArray);
            }
            else
            {
                if (ClientConvert.IsBinaryValue(value))
                {
                    return(ClientConvert.TryKeyBinaryToString(value, out result));
                }
                if (typeof(XElement) == nullableType)
                {
                    result = ((XElement)value).ToString(SaveOptions.None);
                }
                else if (typeof(DateTimeOffset) == nullableType)
                {
                    result = XmlConvert.ToString((DateTimeOffset)value);
                }
                else if (typeof(TimeSpan) == nullableType)
                {
                    result = XmlConvert.ToString((TimeSpan)value);
                }
                else if (typeof(Geography).IsAssignableFrom(nullableType))
                {
                    result = WellKnownTextSqlFormatter.Create(true).Write((Geography)value);
                }
                else if (typeof(Geometry).IsAssignableFrom(nullableType))
                {
                    result = WellKnownTextSqlFormatter.Create(true).Write((Geometry)value);
                }
                else
                {
                    result = null;
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        internal static bool TryXmlPrimitiveToString(object value, out string result)
        {
            Debug.Assert(value != null, "value != null");
            result = null;

            Type valueType = value.GetType();

            valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;

            if (typeof(String) == valueType)
            {
                result = (string)value;
            }
            else if (typeof(Boolean) == valueType)
            {
                result = XmlConvert.ToString((bool)value);
            }
            else if (typeof(Byte) == valueType)
            {
                result = XmlConvert.ToString((byte)value);
            }
            else if (typeof(DateTime) == valueType)
            {
                result = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
            }
            else if (typeof(Decimal) == valueType)
            {
                result = XmlConvert.ToString((decimal)value);
            }
            else if (typeof(Double) == valueType)
            {
                result = XmlConvert.ToString((double)value);
            }
            else if (typeof(Guid) == valueType)
            {
                result = value.ToString();
            }
            else if (typeof(Int16) == valueType)
            {
                result = XmlConvert.ToString((Int16)value);
            }
            else if (typeof(Int32) == valueType)
            {
                result = XmlConvert.ToString((Int32)value);
            }
            else if (typeof(Int64) == valueType)
            {
                result = XmlConvert.ToString((Int64)value);
            }
            else if (typeof(SByte) == valueType)
            {
                result = XmlConvert.ToString((SByte)value);
            }
            else if (typeof(Single) == valueType)
            {
                result = XmlConvert.ToString((Single)value);
            }
            else if (typeof(byte[]) == valueType)
            {
                byte[] byteArray = (byte[])value;
                result = Convert.ToBase64String(byteArray);
            }
            #if !ASTORIA_LIGHT
            else if (ClientConvert.IsBinaryValue(value))
            {
                return(ClientConvert.TryKeyBinaryToString(value, out result));
            }
            #endif
            else if (typeof(System.Xml.Linq.XElement) == valueType)
            {
                result = ((System.Xml.Linq.XElement)value).ToString(System.Xml.Linq.SaveOptions.None);
            }
            else
            {
                result = null;
                return(false);
            }

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