コード例 #1
0
        public static byte[] ConvertKey <TData>(TData data)
        {
            byte[] ret = null;

            Type td = typeof(TData);

            if (data == null)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.KEY_CANT_BE_NULL, td.ToString(), null);
            }

            try
            {
                if (td == TYPE_BYTE_ARRAY)
                {
                    ret = ((byte[])((object)data));
                }
                else if (td == TYPE_ULONG)
                {
                    ret = ((ulong)((object)data)).To_8_bytes_array_BigEndian();
                }
                else if (td == TYPE_DATETIME)
                {
                    ret = ((DateTime)((object)data)).To_8_bytes_array();
                }
                else if (td == TYPE_STRING)
                {
                    ret = new DbUTF8((string)((object)data)).GetBytes();
                }
                else if (td == TYPE_UINT)
                {
                    ret = ((uint)((object)data)).To_4_bytes_array_BigEndian();
                }
                else if (td == TYPE_DECIMAL)
                {
                    ret = ((decimal)((object)data)).To_15_bytes_array_BigEndian();
                }
                else if (td == TYPE_INT)
                {
                    ret = ((int)((object)data)).To_4_bytes_array_BigEndian();
                }
                else if (td == TYPE_LONG)
                {
                    ret = ((long)((object)data)).To_8_bytes_array_BigEndian();
                }
                else if (td == TYPE_DOUBLE)
                {
                    ret = ((double)((object)data)).To_9_bytes_array_BigEndian();
                }
                else if (td == TYPE_FLOAT)
                {
                    ret = ((float)((object)data)).To_4_bytes_array_BigEndian();
                }
                else if (td == TYPE_SHORT)
                {
                    ret = ((short)((object)data)).To_2_bytes_array_BigEndian();
                }
                else if (td == TYPE_USHORT)
                {
                    ret = ((ushort)((object)data)).To_2_bytes_array_BigEndian();
                }
                else if (td == TYPE_DB_ASCII)
                {
                    ret = ((DbAscii)((object)data)).GetBytes();
                }
                else if (td == TYPE_DB_UNICODE)
                {
                    ret = ((DbUnicode)((object)data)).GetBytes();
                }
                else if (td == TYPE_DB_UTF8)
                {
                    ret = ((DbUTF8)((object)data)).GetBytes();
                }
                else if (td == TYPE_BYTE)
                {
                    ret = ((byte)((object)data)).To_1_byte_array();
                }
                else if (td == TYPE_SBYTE)
                {
                    ret = ((sbyte)((object)data)).To_1_byte_array();
                }
                else if (td == TYPE_CHAR)
                {
                    ret = ((char)((object)data)).To_2_byte_array();
                }
                else if (td == TYPE_GUID)
                {
                    ret = ((Guid)((object)data)).ToByteArray();
                }
                else if (td.GetTypeInfo().IsEnum)
                {
                    var enumtype = Enum.GetUnderlyingType(td);

                    if (enumtype == TYPE_INT)
                    {
                        ret = ((int)((object)data)).To_4_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_LONG)
                    {
                        ret = ((long)((object)data)).To_8_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_UINT)
                    {
                        ret = ((uint)((object)data)).To_4_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_ULONG)
                    {
                        ret = ((ulong)((object)data)).To_8_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_USHORT)
                    {
                        ret = ((ushort)((object)data)).To_2_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_SHORT)
                    {
                        ret = ((short)((object)data)).To_2_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_BYTE)
                    {
                        ret = ((byte)((object)data)).To_1_byte_array();
                    }
                    else if (enumtype == TYPE_SBYTE)
                    {
                        ret = ((sbyte)((object)data)).To_1_byte_array();
                    }
                    else
                    {
                        throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE, td.ToString(), null);
                    }
                }
                else
                {
                    throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE, td.ToString(), null);
                }
            }
            catch (Exception ex)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE_VALUE, String.Concat(td.ToString(), " ", data.ToString()), ex);
            }

            //Key can be of byte[0]
            //if (ret == null || ret.Length == 0)
            //    throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.KEY_CANT_BE_NULL, td.ToString(), null);

            return(ret);
        }
コード例 #2
0
        //static Type TYPE_DBINTABLE = typeof(DbInTable);


        ////Slower then original
        //public static byte[] ConvertValue<TData>(TData data)
        //{
        //    //byte[] ret = null;

        //    if (data == null)
        //        return null;

        //    Type td = typeof(TData);
        //    string std = td.ToString();



        //    try
        //    {
        //        switch (td.Name)
        //        {
        //            case "DbMJSON`1":
        //            case "DbCustomSerializer`1":
        //            case "DbXML`1":
        //                return ((IDBConvertable)((object)data)).GetBytes();
        //        }

        //        switch (std)
        //        {
        //            case "System.Byte[]":
        //                return ((byte[])((object)data));
        //            case "System.UInt64":
        //            case "System.Nullable`1[System.UInt64]":
        //                return ((ulong)((object)data)).To_8_bytes_array_BigEndian();
        //            case "System.DateTime":
        //            case "System.Nullable`1[System.DateTime]":
        //                return ((ulong)(((DateTime)((object)data)).Ticks)).To_8_bytes_array_BigEndian();
        //            case "System.String":
        //                return new DbUTF8((string)((object)data)).GetBytes();
        //            case "System.UInt32":
        //            case "System.Nullable`1[System.UInt32]":
        //                return ((uint)((object)data)).To_4_bytes_array_BigEndian();
        //            case "System.Decimal":
        //            case "System.Nullable`1[System.Decimal]":
        //                return ((decimal)((object)data)).To_15_bytes_array_BigEndian();
        //            case "System.Double":
        //            case "System.Nullable`1[System.Double]":
        //                return ((double)((object)data)).To_9_bytes_array_BigEndian();
        //            case "System.Single":
        //            case "System.Nullable`1[System.Single]":
        //                return ((float)((object)data)).To_4_bytes_array_BigEndian();
        //            case "System.Int32":
        //            case "System.Nullable`1[System.Int32]":
        //                return ((int)((object)data)).To_4_bytes_array_BigEndian();
        //            case "System.Int64":
        //            case "System.Nullable`1[System.Int64]":
        //                return ((long)((object)data)).To_8_bytes_array_BigEndian();
        //            case "System.Int16":
        //            case "System.Nullable`1[System.Int16]":
        //                return ((short)((object)data)).To_2_bytes_array_BigEndian();
        //            case "System.UInt16":
        //            case "System.Nullable`1[System.UInt16]":
        //                return ((ushort)((object)data)).To_2_bytes_array_BigEndian();
        //            case "DBreeze.DataTypes.DbAscii":
        //                return ((DbAscii)((object)data)).GetBytes();
        //            case "DBreeze.DataTypes.DbUnicode":
        //                return ((DbUnicode)((object)data)).GetBytes();
        //            case "DBreeze.DataTypes.DbUTF8":
        //                return ((DbUTF8)((object)data)).GetBytes();
        //            case "System.Byte":
        //            case "System.Nullable`1[System.Byte]":
        //                return new byte[] { ((byte)((object)data)) };
        //            case "System.SByte":
        //            case "System.Nullable`1[System.SByte]":
        //                return ((sbyte)((object)data)).To_1_byte_array();
        //            case "System.Boolean":
        //            case "System.Nullable`1[System.Boolean]":
        //                return ((bool)((object)data)).To_1_byte_array();
        //        }

        //        throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE, td.ToString(), null);


        //    }
        //    catch (Exception ex)
        //    {
        //        throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE_VALUE, String.Concat(td.ToString(), " ", data.ToString()), ex);
        //    }

        //    //return ret;
        //}



        public static byte[] ConvertValue <TData>(TData data)
        {
            byte[] ret = null;

            if (data == null)
            {
                return(null);
            }

            Type td = typeof(TData);

            try
            {
                if (td == TYPE_BYTE_ARRAY)
                {
                    ret = ((byte[])((object)data));
                }
                else if (td.Name == "DbMJSON`1" || td.Name == "DbCustomSerializer`1" || td.Name == "DbXML`1")
                {
                    return(((IDBConvertable)((object)data)).GetBytes());
                }
                else if (td == TYPE_ULONG)
                {
                    ret = ((ulong)((object)data)).To_8_bytes_array_BigEndian();
                }
                else if (td == TYPE_ULONG_NULL)
                {
                    ret = ((ulong?)((object)data)).To_9_bytes_array_BigEndian();
                }
                else if (td == TYPE_DATETIME)
                {
                    ret = ((DateTime)((object)data)).To_8_bytes_array();
                }
                else if (td == TYPE_DATETIME_NULL)
                {
                    ret = ((DateTime?)((object)data)).To_9_bytes_array();
                }
                else if (td == TYPE_STRING)
                {
                    ret = new DbUTF8((string)((object)data)).GetBytes();
                }
                else if (td == TYPE_UINT)
                {
                    ret = ((uint)((object)data)).To_4_bytes_array_BigEndian();
                }
                else if (td == TYPE_UINT_NULL)
                {
                    ret = ((uint?)((object)data)).To_5_bytes_array_BigEndian();
                }
                else if (td == TYPE_DECIMAL)
                {
                    ret = ((decimal)((object)data)).To_15_bytes_array_BigEndian();
                }
                else if (td == TYPE_DECIMAL_NULL)
                {
                    ret = ((decimal?)((object)data)).To_16_bytes_array_BigEndian();
                }
                else if (td == TYPE_DOUBLE)
                {
                    ret = ((double)((object)data)).To_9_bytes_array_BigEndian();
                }
                else if (td == TYPE_DOUBLE_NULL)
                {
                    ret = ((double?)((object)data)).To_10_bytes_array_BigEndian();
                }
                else if (td == TYPE_FLOAT)
                {
                    ret = ((float)((object)data)).To_4_bytes_array_BigEndian();
                }
                else if (td == TYPE_FLOAT_NULL)
                {
                    ret = ((float?)((object)data)).To_5_bytes_array_BigEndian();
                }
                else if (td == TYPE_INT)
                {
                    ret = ((int)((object)data)).To_4_bytes_array_BigEndian();
                }
                else if (td == TYPE_INT_NULL)
                {
                    ret = ((int?)((object)data)).To_5_bytes_array_BigEndian();
                }
                else if (td == TYPE_LONG)
                {
                    ret = ((long)((object)data)).To_8_bytes_array_BigEndian();
                }
                else if (td == TYPE_LONG_NULL)
                {
                    ret = ((long?)((object)data)).To_9_bytes_array_BigEndian();
                }
                else if (td == TYPE_SHORT)
                {
                    ret = ((short)((object)data)).To_2_bytes_array_BigEndian();
                }
                else if (td == TYPE_SHORT_NULL)
                {
                    ret = ((short?)((object)data)).To_3_bytes_array_BigEndian();
                }
                else if (td == TYPE_USHORT)
                {
                    ret = ((ushort)((object)data)).To_2_bytes_array_BigEndian();
                }
                else if (td == TYPE_USHORT_NULL)
                {
                    ret = ((ushort?)((object)data)).To_3_bytes_array_BigEndian();
                }
                else if (td == TYPE_DB_ASCII)
                {
                    ret = ((DbAscii)((object)data)).GetBytes();
                }
                else if (td == TYPE_DB_UNICODE)
                {
                    ret = ((DbUnicode)((object)data)).GetBytes();
                }
                else if (td == TYPE_DB_UTF8)
                {
                    ret = ((DbUTF8)((object)data)).GetBytes();
                }
                else if (td == TYPE_BYTE)
                {
                    ret = ((byte)((object)data)).To_1_byte_array();
                }
                else if (td == TYPE_BYTE_NULL)
                {
                    ret = ((byte?)((object)data)).To_2_byte_array();
                }
                else if (td == TYPE_SBYTE)
                {
                    ret = ((sbyte)((object)data)).To_1_byte_array();
                }
                else if (td == TYPE_SBYTE_NULL)
                {
                    ret = ((sbyte?)((object)data)).To_2_byte_array();
                }
                else if (td == TYPE_BOOL)
                {
                    ret = ((bool)((object)data)).To_1_byte_array();
                }
                else if (td == TYPE_BOOL_NULL)
                {
                    ret = ((bool?)((object)data)).To_1_byte_array();
                }
                else if (td == TYPE_CHAR)
                {
                    ret = ((char)((object)data)).To_2_byte_array();
                }
                else if (td == TYPE_CHAR_NULL)
                {
                    ret = ((char?)((object)data)).To_3_byte_array();
                }
                else if (td == TYPE_GUID)
                {
                    ret = ((Guid)((object)data)).ToByteArray();
                }
                else if (td == TYPE_OBJECT)
                {
                    throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE, td.ToString(), null);
                }
                else if (td.GetTypeInfo().IsEnum)
                {
                    var enumtype = Enum.GetUnderlyingType(td);

                    if (enumtype == TYPE_INT)
                    {
                        ret = ((int)((object)data)).To_4_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_LONG)
                    {
                        ret = ((long)((object)data)).To_8_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_UINT)
                    {
                        ret = ((uint)((object)data)).To_4_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_ULONG)
                    {
                        ret = ((ulong)((object)data)).To_8_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_USHORT)
                    {
                        ret = ((ushort)((object)data)).To_2_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_SHORT)
                    {
                        ret = ((short)((object)data)).To_2_bytes_array_BigEndian();
                    }
                    else if (enumtype == TYPE_BYTE)
                    {
                        ret = ((byte)((object)data)).To_1_byte_array();
                    }
                    else if (enumtype == TYPE_SBYTE)
                    {
                        ret = ((sbyte)((object)data)).To_1_byte_array();
                    }
                    else
                    {
                        throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE, td.ToString(), null);
                    }
                }
                else
                {
                    //Trying byte serialization for unknown object, in case if byte serializer is set
                    if (CustomSerializator.ByteArraySerializator != null)
                    {
                        return(CustomSerializator.ByteArraySerializator(data));
                    }

                    throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE, td.ToString(), null);
                }
            }
            catch (Exception ex)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.UNSUPPORTED_DATATYPE_VALUE, String.Concat(td.ToString(), " ", data.ToString()), ex);
            }

            return(ret);
        }