Пример #1
0
        public virtual byte[] CreateKey()
        {
            var length = Encoding.UTF8.GetByteCount(Key);
            var buffer = new byte[length];

            Converter.FromString(Key, buffer, 0);
            return(buffer);
        }
        /// <summary>
        /// Writes a <see cref="String" /> to a new buffer.
        /// </summary>
        /// <param name="converter">The converter.</param>
        /// <param name="value">The value.</param>
        public static byte[] FromString(this IByteConverter converter, string value)
        {
            var buffer = new byte[converter.GetStringByteCount(value)];

            converter.FromString(value, buffer);

            return(buffer);
        }
Пример #3
0
        public byte[] Serialize <T>(T value)
        {
            var bytes    = new byte[] { };
            var typeCode = Type.GetTypeCode(typeof(T));

            switch (typeCode)
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:
            case TypeCode.String:
            case TypeCode.Char:
                _converter.FromString(Convert.ToString(value), ref bytes, 0);
                break;

            case TypeCode.Int16:
                _converter.FromInt16(Convert.ToInt16(value), ref bytes, 0);
                break;

            case TypeCode.UInt16:
                _converter.FromUInt16(Convert.ToUInt16(value), ref bytes, 0);
                break;

            case TypeCode.Int32:
                _converter.FromInt32(Convert.ToInt32(value), ref bytes, 0);
                break;

            case TypeCode.UInt32:
                _converter.FromUInt32(Convert.ToUInt32(value), ref bytes, 0);
                break;

            case TypeCode.Int64:
                _converter.FromInt64(Convert.ToInt64(value), ref bytes, 0);
                break;

            case TypeCode.UInt64:
                _converter.FromUInt64(Convert.ToUInt64(value), ref bytes, 0);
                break;

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.DateTime:
            case TypeCode.Boolean:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Object:
                bytes = SerializeAsJson(value);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(bytes);
        }