internal static object StringToObject(Type type, string value) { if (value == null || value == String.Empty) { return(DBNull.Value); } switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: return(XmlConvert.ToBoolean(value)); case TypeCode.Byte: return(XmlConvert.ToByte(value)); case TypeCode.Char: return((char)XmlConvert.ToInt32(value)); #if NET_2_0 case TypeCode.DateTime: return(XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified)); #else case TypeCode.DateTime: return(XmlConvert.ToDateTime(value)); #endif case TypeCode.Decimal: return(XmlConvert.ToDecimal(value)); case TypeCode.Double: return(XmlConvert.ToDouble(value)); case TypeCode.Int16: return(XmlConvert.ToInt16(value)); case TypeCode.Int32: return(XmlConvert.ToInt32(value)); case TypeCode.Int64: return(XmlConvert.ToInt64(value)); case TypeCode.SByte: return(XmlConvert.ToSByte(value)); case TypeCode.Single: return(XmlConvert.ToSingle(value)); case TypeCode.UInt16: return(XmlConvert.ToUInt16(value)); case TypeCode.UInt32: return(XmlConvert.ToUInt32(value)); case TypeCode.UInt64: return(XmlConvert.ToUInt64(value)); } if (type == typeof(TimeSpan)) { return(XmlConvert.ToTimeSpan(value)); } if (type == typeof(Guid)) { return(XmlConvert.ToGuid(value)); } if (type == typeof(byte[])) { return(Convert.FromBase64String(value)); } return(Convert.ChangeType(value, type)); }
public static UInt16 StringToUnsignedShort(string value) { return(XmlConvert.ToUInt16(value)); }