예제 #1
0
        /// <summary>Formats the specified object into an (unescaped) value suitable for a uri.</summary>
        /// <param name="o">Object value.</param>
        /// <returns></returns>
        public static string FormatForKey(object o, bool?useSmallCasing, bool useDoublePostFix)
        {
            if (o == null)
            {
                return("");
            }
            else
            {
                string       result;
                IFormattable formattable = o as IFormattable;
                if (o is DateTime)
                {
                    result = XmlConvert.ToString(ConvertDateTimeToDateTimeOffset((DateTime)o));
                }
                else if (o is DateTimeOffset)
                {
                    result = XmlConvert.ToString((DateTimeOffset)o);
                }
                else if (o is Guid)
                {
                    result = XmlConvert.ToString((Guid)o);
                }
                else if (o is Int64)
                {
                    result = System.Xml.XmlConvert.ToString((Int64)o);
                    if (useSmallCasing.HasValue)
                    {
                        result += (useSmallCasing.Value ? "l" : "L");
                    }
                    else
                    {
                        result += "L";
                    }
                }
                else if (o is decimal)
                {
                    result = System.Xml.XmlConvert.ToString((decimal)o);
                    if (useSmallCasing.HasValue)
                    {
                        result += (useSmallCasing.HasValue ? "m" : "M");
                    }
                    else
                    {
                        result += "M";
                    }
                }
                else if (o is float)
                {
                    float f = (float)o;
                    result = System.Xml.XmlConvert.ToString(f);
                    if (!float.IsInfinity(f) && !float.IsNaN(f) && !result.Contains(".") && !result.Contains("E"))
                    {
                        result += ".0";
                    }
                    if (useSmallCasing.HasValue)
                    {
                        result += (useSmallCasing.Value ? "f" : "F");
                    }
                    else
                    {
                        result += "f";
                    }
                }
                else if (o is Single)
                {
                    Single s = (Single)o;
                    result = System.Xml.XmlConvert.ToString(s);
                    if (!Single.IsInfinity(s) && !Single.IsNaN(s) && !result.Contains(".") && !result.Contains("E"))
                    {
                        result += ".0";
                    }
                    if (useSmallCasing.HasValue)
                    {
                        result += (useSmallCasing.Value ? "f" : "F");
                    }
                    else
                    {
                        result += "f";
                    }
                }
                else if (o is TimeSpan)
                {
                    if (useSmallCasing.HasValue)
                    {
                        result = (useSmallCasing.Value ? "duration'" : "Duration'");
                    }
                    else
                    {
                        result = "duration'";
                    }
                    result += XmlConvert.ToString((TimeSpan)o) + "'";
                }
                else if (o is Double)
                {
                    Double d = (Double)o;
                    result = System.Xml.XmlConvert.ToString(d);
                    if (!Double.IsInfinity(d) && !Double.IsNaN(d) && !result.Contains(".") && !result.Contains("E"))
                    {
                        result += ".0";
                    }
                    if (useDoublePostFix && !Double.IsInfinity(d) && !Double.IsNaN(d))
                    {
                        if (useSmallCasing.HasValue)
                        {
                            result += (useSmallCasing.Value ? "d" : "D");
                        }
                        else
                        {
                            result += "D";
                        }
                    }
                    else
                    {
                        result = UriQueryBuilder.UrlEncodeString(result);
                    }
                }
                else if (o is bool)
                {
                    result = ((bool)o) ? "true" : "false";
                }
#if !ClientSKUFramework
                else if (o is Binary)
                {
                    byte[] bytes = (o == null) ? null : ((Binary)o).ToArray();
                    result = FormatByteArrayForKey(bytes, useSmallCasing);
                }
#endif

                else if (o is byte[])
                {
                    byte[] bytes = (byte[])o;
                    result = FormatByteArrayForKey(bytes, useSmallCasing);
                }
                else
                {
                    result = o.ToString();
                }

                if (o is string || o is XElement)
                {
                    result = "'" + result.Replace("'", "''") + "'";
                }

                return(result);
            }
        }