Exemplo n.º 1
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is byte[] && ((byte[])value).Length == 16)
            {
                return(CassandraConversionHelper.ConvertBytesToGuid((byte[])value));
            }

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

            return(null);
        }
        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);
            }
        }