public static ConvertToSqlChars ( object value ) : |
||
value | object | |
Résultat |
// this should not be called for XmlSerialization public static object ChangeType2(object value, StorageType stype, Type type, IFormatProvider formatProvider) { switch (stype) { // if destination is SQL type case StorageType.SqlBinary: return(SqlConvert.ConvertToSqlBinary(value)); case StorageType.SqlBoolean: return(SqlConvert.ConvertToSqlBoolean(value)); case StorageType.SqlByte: return(SqlConvert.ConvertToSqlByte(value)); case StorageType.SqlBytes: return(SqlConvert.ConvertToSqlBytes(value)); case StorageType.SqlChars: return(SqlConvert.ConvertToSqlChars(value)); case StorageType.SqlDateTime: return(SqlConvert.ConvertToSqlDateTime(value)); case StorageType.SqlDecimal: return(SqlConvert.ConvertToSqlDecimal(value)); case StorageType.SqlDouble: return(SqlConvert.ConvertToSqlDouble(value)); case StorageType.SqlGuid: return(SqlConvert.ConvertToSqlGuid(value)); case StorageType.SqlInt16: return(SqlConvert.ConvertToSqlInt16(value)); case StorageType.SqlInt32: return(SqlConvert.ConvertToSqlInt32(value)); case StorageType.SqlInt64: return(SqlConvert.ConvertToSqlInt64(value)); case StorageType.SqlMoney: return(SqlConvert.ConvertToSqlMoney(value)); case StorageType.SqlSingle: return(SqlConvert.ConvertToSqlSingle(value)); case StorageType.SqlString: return(SqlConvert.ConvertToSqlString(value)); /* case StorageType.SqlXml: * if (DataStorage.IsObjectNull(value)) { * return SqlXml.Null; * } * goto default; */ default: // destination is CLR if ((DBNull.Value == value) || (null == value)) { return(DBNull.Value); } Type valueType = value.GetType(); StorageType vtype = DataStorage.GetStorageType(valueType); // destination is CLR switch (vtype) { // and source is SQL type case StorageType.SqlBinary: case StorageType.SqlBoolean: case StorageType.SqlByte: case StorageType.SqlBytes: case StorageType.SqlChars: case StorageType.SqlDateTime: case StorageType.SqlDecimal: case StorageType.SqlDouble: case StorageType.SqlGuid: case StorageType.SqlInt16: case StorageType.SqlInt32: case StorageType.SqlInt64: case StorageType.SqlMoney: case StorageType.SqlSingle: case StorageType.SqlString: throw ExceptionBuilder.ConvertFailed(valueType, type); default: // source is CLR type if (StorageType.String == stype) { // destination is string switch (vtype) { // source's type case StorageType.Boolean: return(((IConvertible)(bool)value).ToString(formatProvider)); case StorageType.Char: return(((IConvertible)(char)value).ToString(formatProvider)); case StorageType.SByte: return(((sbyte)value).ToString(formatProvider)); case StorageType.Byte: return(((byte)value).ToString(formatProvider)); case StorageType.Int16: return(((short)value).ToString(formatProvider)); case StorageType.UInt16: return(((ushort)value).ToString(formatProvider)); case StorageType.Int32: return(((int)value).ToString(formatProvider)); case StorageType.UInt32: return(((uint)value).ToString(formatProvider)); case StorageType.Int64: return(((long)value).ToString(formatProvider)); case StorageType.UInt64: return(((ulong)value).ToString(formatProvider)); case StorageType.Single: return(((float)value).ToString(formatProvider)); case StorageType.Double: return(((double)value).ToString(formatProvider)); case StorageType.Decimal: return(((decimal)value).ToString(formatProvider)); case StorageType.DateTime: return(((DateTime)value).ToString(formatProvider)); //return XmlConvert.ToString((DateTime) value, XmlDateTimeSerializationMode.RoundtripKind); case StorageType.TimeSpan: return(XmlConvert.ToString((TimeSpan)value)); case StorageType.Guid: return(XmlConvert.ToString((Guid)value)); case StorageType.String: return((string)value); case StorageType.CharArray: return(new string((char[])value)); case StorageType.DateTimeOffset: return(((DateTimeOffset)value).ToString(formatProvider)); case StorageType.BigInteger: break; default: IConvertible iconvertible = (value as IConvertible); if (null != iconvertible) { return(iconvertible.ToString(formatProvider)); } // catch additional classes like Guid IFormattable iformattable = (value as IFormattable); if (null != iformattable) { return(iformattable.ToString(null, formatProvider)); } return(value.ToString()); } } else if (StorageType.TimeSpan == stype) { // destination is TimeSpan switch (vtype) { case StorageType.String: return(XmlConvert.ToTimeSpan((string)value)); case StorageType.Int32: return(new TimeSpan((int)value)); case StorageType.Int64: return(new TimeSpan((long)value)); default: return((TimeSpan)value); } } else if (StorageType.DateTimeOffset == stype) { // destination is DateTimeOffset return((DateTimeOffset)value); } else if (StorageType.String == vtype) { // if source is string switch (stype) { // type of destination case StorageType.String: return((string)value); case StorageType.Boolean: if ("1" == (string)value) { return(true); } if ("0" == (string)value) { return(false); } break; case StorageType.Char: return(((IConvertible)(string)value).ToChar(formatProvider)); case StorageType.SByte: return(((IConvertible)(string)value).ToSByte(formatProvider)); case StorageType.Byte: return(((IConvertible)(string)value).ToByte(formatProvider)); case StorageType.Int16: return(((IConvertible)(string)value).ToInt16(formatProvider)); case StorageType.UInt16: return(((IConvertible)(string)value).ToUInt16(formatProvider)); case StorageType.Int32: return(((IConvertible)(string)value).ToInt32(formatProvider)); case StorageType.UInt32: return(((IConvertible)(string)value).ToUInt32(formatProvider)); case StorageType.Int64: return(((IConvertible)(string)value).ToInt64(formatProvider)); case StorageType.UInt64: return(((IConvertible)(string)value).ToUInt64(formatProvider)); case StorageType.Single: return(((IConvertible)(string)value).ToSingle(formatProvider)); case StorageType.Double: return(((IConvertible)(string)value).ToDouble(formatProvider)); case StorageType.Decimal: return(((IConvertible)(string)value).ToDecimal(formatProvider)); case StorageType.DateTime: return(((IConvertible)(string)value).ToDateTime(formatProvider)); //return XmlConvert.ToDateTime((string) value, XmlDateTimeSerializationMode.RoundtripKind); case StorageType.TimeSpan: return(XmlConvert.ToTimeSpan((string)value)); case StorageType.Guid: return(XmlConvert.ToGuid((string)value)); case StorageType.Uri: return(new Uri((string)value)); default: // other clr types, break; } } return(Convert.ChangeType(value, type, formatProvider)); } } }