Exemplo n.º 1
0
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (!(value is BigInteger))
            {
                return(null);
            }

            if (destinationType == typeof(byte[]))
            {
                var buffer = ((BigInteger)value).ToByteArray();
                return(CassandraConversionHelper.ConvertEndian(buffer));
            }

            if (destinationType == typeof(BigInteger))
            {
                return((BigInteger)value);
            }

            if (destinationType == typeof(byte))
            {
                return((byte)(BigInteger)value);
            }
            if (destinationType == typeof(short))
            {
                return((short)(BigInteger)value);
            }
            if (destinationType == typeof(int))
            {
                return((int)(BigInteger)value);
            }
            if (destinationType == typeof(long))
            {
                return((long)(BigInteger)value);
            }
            if (destinationType == typeof(sbyte))
            {
                return((sbyte)(BigInteger)value);
            }
            if (destinationType == typeof(ushort))
            {
                return((ushort)(BigInteger)value);
            }
            if (destinationType == typeof(uint))
            {
                return((uint)(BigInteger)value);
            }
            if (destinationType == typeof(ulong))
            {
                return((ulong)(BigInteger)value);
            }

            return(null);
        }
Exemplo n.º 2
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is byte[])
            {
                var buffer = CassandraConversionHelper.ConvertEndian((byte[])value);
                return(new BigInteger(buffer));
            }

            if (value is BigInteger)
            {
                return((BigInteger)value);
            }

            if (value is byte)
            {
                return((BigInteger)(byte)value);
            }
            if (value is short)
            {
                return((BigInteger)(short)value);
            }
            if (value is int)
            {
                return((BigInteger)(int)value);
            }
            if (value is long)
            {
                return((BigInteger)(long)value);
            }
            if (value is sbyte)
            {
                return((BigInteger)(sbyte)value);
            }
            if (value is ushort)
            {
                return((BigInteger)(ushort)value);
            }
            if (value is uint)
            {
                return((BigInteger)(uint)value);
            }
            if (value is ulong)
            {
                return((BigInteger)(ulong)value);
            }

            return(null);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is Guid)
            {
                return(CassandraConversionHelper.ConvertGuidToBytes((Guid)value));
            }

            byte[] bytes = null;

            if (value is byte[])
            {
                bytes = (byte[])value;
            }
            else if (value is DateTimeOffset)
            {
                bytes = BitConverter.GetBytes(((DateTimeOffset)value).UtcTicks);
            }

            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.DateTime:
                    bytes = BitConverter.GetBytes(((DateTime)value).Ticks); 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(CassandraConversionHelper.ConvertEndian(bytes));
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (!(value is byte[]))
            {
                return(null);
            }

            if (destinationType == typeof(Guid))
            {
                return(CassandraConversionHelper.ConvertBytesToGuid((byte[])value));
            }

            var bytes = CassandraConversionHelper.ConvertEndian((byte[])value);

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

            if (destinationType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset(BitConverter.ToInt64(bytes, 0), new TimeSpan(0L)));
            }

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

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

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

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

            case TypeCode.Char:
                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);
            }
        }