コード例 #1
0
        public static object ChangeType2(
            object value,
            StorageType stype,
            Type type,
            IFormatProvider formatProvider)
        {
            switch (stype)
            {
            case StorageType.SqlBinary:
                return((object)SqlConvert.ConvertToSqlBinary(value));

            case StorageType.SqlBoolean:
                return((object)SqlConvert.ConvertToSqlBoolean(value));

            case StorageType.SqlByte:
                return((object)SqlConvert.ConvertToSqlByte(value));

            case StorageType.SqlBytes:
                return((object)SqlConvert.ConvertToSqlBytes(value));

            case StorageType.SqlChars:
                return((object)SqlConvert.ConvertToSqlChars(value));

            case StorageType.SqlDateTime:
                return((object)SqlConvert.ConvertToSqlDateTime(value));

            case StorageType.SqlDecimal:
                return((object)SqlConvert.ConvertToSqlDecimal(value));

            case StorageType.SqlDouble:
                return((object)SqlConvert.ConvertToSqlDouble(value));

            case StorageType.SqlGuid:
                return((object)SqlConvert.ConvertToSqlGuid(value));

            case StorageType.SqlInt16:
                return((object)SqlConvert.ConvertToSqlInt16(value));

            case StorageType.SqlInt32:
                return((object)SqlConvert.ConvertToSqlInt32(value));

            case StorageType.SqlInt64:
                return((object)SqlConvert.ConvertToSqlInt64(value));

            case StorageType.SqlMoney:
                return((object)SqlConvert.ConvertToSqlMoney(value));

            case StorageType.SqlSingle:
                return((object)SqlConvert.ConvertToSqlSingle(value));

            case StorageType.SqlString:
                return((object)SqlConvert.ConvertToSqlString(value));

            default:
                if (DBNull.Value == value || value == null)
                {
                    return((object)DBNull.Value);
                }
                Type        type1       = value.GetType();
                StorageType storageType = DataStorageHelper.GetStorageType(type1);
                switch (storageType)
                {
                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 InvalidExpressionException.SqlConvertFailed(type1, type);

                default:
                    if (StorageType.String != stype)
                    {
                        if (StorageType.TimeSpan != stype)
                        {
                            if (StorageType.String == storageType)
                            {
                                switch (stype)
                                {
                                case StorageType.Boolean:
                                    if (!("1" != (string)value))
                                    {
                                        return((object)true);
                                    }
                                    if ("0" == (string)value)
                                    {
                                        return((object)false);
                                    }
                                    break;

                                case StorageType.Char:
                                    return((object)((IConvertible)(string)value).ToChar(formatProvider));

                                case StorageType.SByte:
                                    return((object)((IConvertible)(string)value).ToSByte(formatProvider));

                                case StorageType.Byte:
                                    return((object)((IConvertible)(string)value).ToByte(formatProvider));

                                case StorageType.Int16:
                                    return((object)((IConvertible)(string)value).ToInt16(formatProvider));

                                case StorageType.UInt16:
                                    return((object)((IConvertible)(string)value).ToUInt16(formatProvider));

                                case StorageType.Int32:
                                    return((object)((IConvertible)(string)value).ToInt32(formatProvider));

                                case StorageType.UInt32:
                                    return((object)((IConvertible)(string)value).ToUInt32(formatProvider));

                                case StorageType.Int64:
                                    return((object)((IConvertible)(string)value).ToInt64(formatProvider));

                                case StorageType.UInt64:
                                    return((object)((IConvertible)(string)value).ToUInt64(formatProvider));

                                case StorageType.Single:
                                    return((object)((IConvertible)(string)value).ToSingle(formatProvider));

                                case StorageType.Double:
                                    return((object)((IConvertible)(string)value).ToDouble(formatProvider));

                                case StorageType.Decimal:
                                    return((object)((IConvertible)(string)value).ToDecimal(formatProvider));

                                case StorageType.DateTime:
                                    return((object)((IConvertible)(string)value).ToDateTime(formatProvider));

                                case StorageType.TimeSpan:
                                    return((object)XmlConvert.ToTimeSpan((string)value));

                                case StorageType.String:
                                    return((object)(string)value);

                                case StorageType.Guid:
                                    return((object)XmlConvert.ToGuid((string)value));

                                case StorageType.Uri:
                                    return((object)new Uri((string)value));
                                }
                            }
                            return(Convert.ChangeType(value, type, formatProvider));
                        }
                        switch (storageType)
                        {
                        case StorageType.Int32:
                            return((object)new TimeSpan((long)(int)value));

                        case StorageType.Int64:
                            return((object)new TimeSpan((long)value));

                        case StorageType.String:
                            return((object)XmlConvert.ToTimeSpan((string)value));

                        default:
                            return((object)(TimeSpan)value);
                        }
                    }
                    else
                    {
                        switch (storageType)
                        {
                        case StorageType.Boolean:
                            return((object)((bool)value).ToString(formatProvider));

                        case StorageType.Char:
                            return((object)((char)value).ToString(formatProvider));

                        case StorageType.SByte:
                            return((object)((sbyte)value).ToString(formatProvider));

                        case StorageType.Byte:
                            return((object)((byte)value).ToString(formatProvider));

                        case StorageType.Int16:
                            return((object)((short)value).ToString(formatProvider));

                        case StorageType.UInt16:
                            return((object)((ushort)value).ToString(formatProvider));

                        case StorageType.Int32:
                            return((object)((int)value).ToString(formatProvider));

                        case StorageType.UInt32:
                            return((object)((uint)value).ToString(formatProvider));

                        case StorageType.Int64:
                            return((object)((long)value).ToString(formatProvider));

                        case StorageType.UInt64:
                            return((object)((ulong)value).ToString(formatProvider));

                        case StorageType.Single:
                            return((object)((float)value).ToString(formatProvider));

                        case StorageType.Double:
                            return((object)((double)value).ToString(formatProvider));

                        case StorageType.Decimal:
                            return((object)((Decimal)value).ToString(formatProvider));

                        case StorageType.DateTime:
                            return((object)((DateTime)value).ToString(formatProvider));

                        case StorageType.TimeSpan:
                            return((object)XmlConvert.ToString((TimeSpan)value));

                        case StorageType.String:
                            return((object)(string)value);

                        case StorageType.Guid:
                            return((object)XmlConvert.ToString((Guid)value));

                        case StorageType.CharArray:
                            return((object)new string((char[])value));

                        default:
                            IConvertible convertible = value as IConvertible;
                            if (convertible != null)
                            {
                                return((object)convertible.ToString(formatProvider));
                            }
                            IFormattable formattable = value as IFormattable;
                            if (formattable != null)
                            {
                                return((object)formattable.ToString((string)null, formatProvider));
                            }
                            return((object)value.ToString());
                        }
                    }
                }
            }
        }
コード例 #2
0
 public static object ChangeType(object value, Type type, IFormatProvider formatProvider)
 {
     return(SqlConvert.ChangeType2(value, DataStorageHelper.GetStorageType(type), type, formatProvider));
 }