コード例 #1
0
        public override object ConvertToInternal(byte[] value, Type destinationType)
        {
            if (destinationType == typeof(Guid))
            {
                return(new Guid(value));
            }

            if (destinationType == typeof(byte[]))
            {
                return(value);
            }

            var bytes = value;

            if (destinationType == typeof(DateTimeOffset) || destinationType == typeof(DateTime))
            {
                var dto = DateTypeConverter.FromUnixTime(BitConverter.ToInt64(bytes, 0));

                if (destinationType == typeof(DateTime))
                {
                    return(dto.LocalDateTime);
                }

                return(dto);
            }

            if (destinationType == typeof(BigInteger))
            {
                return(new BigInteger(bytes));
            }

            if (destinationType == typeof(BigDecimal))
            {
                return(new BigDecimal(bytes));
            }

            if (destinationType == typeof(char[]))
            {
                return(value.Cast <char>().ToArray());
            }

            switch (Type.GetTypeCode(destinationType))
            {
            case TypeCode.Byte:
                return(bytes[0]);

            case TypeCode.SByte:
                return(Convert.ToSByte(bytes[0]));

            case TypeCode.Boolean:
                return(BitConverter.ToBoolean(bytes, 0));

            case TypeCode.Char:
                if (bytes.Length < 2)
                {
                    Array.Resize <byte>(ref bytes, 2);
                }

                return(BitConverter.ToChar(bytes, 0));

            case TypeCode.Double:
                return(BitConverter.ToDouble(bytes, 0));

            case TypeCode.Int16:
                return(BitConverter.ToInt16(bytes, 0));

            case TypeCode.Int32:
                return(BitConverter.ToInt32(bytes, 0));

            case TypeCode.Int64:
                return(BitConverter.ToInt64(bytes, 0));

            case TypeCode.Single:
                return(BitConverter.ToSingle(bytes, 0));

            case TypeCode.UInt16:
                return(BitConverter.ToUInt16(bytes, 0));

            case TypeCode.UInt32:
                return(BitConverter.ToUInt32(bytes, 0));

            case TypeCode.UInt64:
                return(BitConverter.ToUInt64(bytes, 0));

            case TypeCode.Decimal:
                return(ToDecimal(bytes));

            case TypeCode.String:
                return(Encoding.UTF8.GetString(bytes));

            default:
                return(null);
            }
        }
コード例 #2
0
        public override byte[] ConvertFromInternal(object value)
        {
            if (value is Guid)
            {
                return(((Guid)value).ToByteArray());
            }

            if (value is byte[])
            {
                return((byte[])value);
            }

            byte[] bytes = null;

            if (value is BigInteger)
            {
                bytes = ((BigInteger)value).ToByteArray();
            }

            if (value is BigDecimal)
            {
                bytes = ((BigDecimal)value).ToByteArray();
            }

            if (value is DateTimeOffset || value is DateTime)
            {
                var dto = DateTimeOffset.MinValue;

                if (value is DateTimeOffset)
                {
                    dto = (DateTimeOffset)value;
                }

                if (value is DateTime)
                {
                    dto = new DateTimeOffset((DateTime)value);
                }

                bytes = BitConverter.GetBytes(DateTypeConverter.ToUnixTime(dto));
            }

            if (value is char[])
            {
                bytes = ((char[])value).Cast <byte>().ToArray();
            }

            if (bytes == null)
            {
                switch (Type.GetTypeCode(value.GetType()))
                {
                case TypeCode.Byte:
                    bytes = new byte[] { (byte)value }; break;

                case TypeCode.SByte:
                    bytes = new byte[] { Convert.ToByte((sbyte)value) }; break;

                case TypeCode.Boolean:
                    bytes = BitConverter.GetBytes((bool)value); break;

                case TypeCode.Char:
                    bytes = BitConverter.GetBytes((char)value); break;

                case TypeCode.Double:
                    bytes = BitConverter.GetBytes((double)value); break;

                case TypeCode.Int16:
                    bytes = BitConverter.GetBytes((short)value); break;

                case TypeCode.Int32:
                    bytes = BitConverter.GetBytes((int)value); break;

                case TypeCode.Int64:
                    bytes = BitConverter.GetBytes((long)value); break;

                case TypeCode.Single:
                    bytes = BitConverter.GetBytes((float)value); break;

                case TypeCode.UInt16:
                    bytes = BitConverter.GetBytes((ushort)value); break;

                case TypeCode.UInt32:
                    bytes = BitConverter.GetBytes((uint)value); break;

                case TypeCode.UInt64:
                    bytes = BitConverter.GetBytes((ulong)value); break;

                case TypeCode.Decimal:
                    bytes = FromDecimal((decimal)value); break;

                case TypeCode.String:
                    bytes = Encoding.UTF8.GetBytes((string)value); break;

                default:
                    break;
                }
            }

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

            return(bytes);
        }