public static object StringToPrimitive(string value, Type primitiveType) { if (value == null) { AstoriaTestLog.IsTrue(primitiveType.IsClass); return(null); } if (primitiveType.IsGenericType && primitiveType.GetGenericTypeDefinition() == typeof(Nullable <>)) { primitiveType = primitiveType.GetGenericArguments()[0]; } //value = GetUnquotedJsonString(value, primitiveType); if (primitiveType == typeof(byte[])) { return(Convert.FromBase64String(value)); } #if !ClientSKUFramework else if (primitiveType == typeof(Binary)) { return(new Binary(Convert.FromBase64String(value))); } #endif else if (primitiveType == typeof(bool)) { return(Convert.ToBoolean(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(byte)) { return(Convert.ToByte(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(DateTime)) { Match m = DateTimeRegex.Match(value); if (m.Success) { string ticksStr = m.Groups["ticks"].Value; long ticks; if (long.TryParse(ticksStr, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out ticks)) { return(new DateTime(ticks * 10000 + DatetimeMinTimeTicks, DateTimeKind.Utc)); } } return(XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.RoundtripKind)); } else if (primitiveType == typeof(Decimal)) { NumberFormatInfo formatInfo = new NumberFormatInfo(); return(Convert.ToDecimal(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(Double)) { double d; if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out d)) { return(d); } return(XmlConvert.ToDouble(value)); // +- INF } else if (primitiveType == typeof(Guid)) { return(new Guid(value)); } else if (primitiveType == typeof(Int16)) { return(Convert.ToInt16(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(Int32)) { return(Convert.ToInt32(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(Int64)) { return(Convert.ToInt64(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(sbyte)) { return(Convert.ToSByte(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(Single)) { Single s; if (Single.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out s)) { return(s); } return(XmlConvert.ToSingle(value)); // +- INF } else if (primitiveType == typeof(string)) { if (value == null) { return("null"); } return(value); } else if (primitiveType == typeof(UInt16)) { return(Convert.ToUInt16(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(UInt32)) { return(Convert.ToUInt32(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(UInt64)) { return(Convert.ToUInt64(value, CultureInfo.InvariantCulture)); } else if (primitiveType == typeof(System.Xml.Linq.XElement)) { return(System.Xml.Linq.XElement.Parse(value, System.Xml.Linq.LoadOptions.PreserveWhitespace)); } else if (primitiveType == typeof(TimeSpan)) { return(XmlConvert.ToTimeSpan(value)); } else if (primitiveType == typeof(DateTimeOffset)) { Match m = DateTimeOffsetRegex.Match(value); if (m.Success) { string ticksStr = m.Groups["ticks"].Value; string offset = m.Groups["offset"].Value; long ticks; Int32 offsetMins; if (long.TryParse(ticksStr, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out ticks) && Int32.TryParse(offset, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out offsetMins)) { return(new DateTimeOffset(ticks * 10000 + DatetimeMinTimeTicks, new TimeSpan(0, offsetMins, 0))); } } return(XmlConvert.ToDateTimeOffset(value)); } throw new Exception(String.Format("Invalid PrimitiveType encountered : {0}", primitiveType.FullName)); }