protected string ReadString(Stream stream)
    {
        var lengthBytes = new byte[TypesInfo.GetBytesCount(typeof(int))];

        stream.Read(lengthBytes, 0, lengthBytes.Length);
        var stringBytesCount = BitConverter.ToInt32(lengthBytes, 0);

        if (stringBytesCount == NullBytesCout)
        {
            return(null);
        }

        var stringBytes = new byte[stringBytesCount];

        stream.Read(stringBytes, 0, stringBytes.Length);
        return(Encoding.GetString(stringBytes));
    }
示例#2
0
        private object ReadValue(Stream stream, Type type)
        {
            if (type == typeof(byte))
            {
                return((byte)stream.ReadByte());
            }
            if (type == typeof(string))
            {
                return(ReadString(stream));
            }

            var bytesCount = TypesInfo.GetBytesCount(type);
            var valueBytes = new byte[bytesCount];

            stream.Read(valueBytes, 0, valueBytes.Length);

            if (type == typeof(bool))
            {
                return(BitConverter.ToBoolean(valueBytes, 0));
            }
            else if (type == typeof(short))
            {
                return(BitConverter.ToInt16(valueBytes, 0));
            }
            else if (type == typeof(int))
            {
                return(BitConverter.ToInt32(valueBytes, 0));
            }
            else if (type == typeof(long))
            {
                return(BitConverter.ToInt64(valueBytes, 0));
            }
            else if (type == typeof(ushort))
            {
                return(BitConverter.ToUInt16(valueBytes, 0));
            }
            else if (type == typeof(uint))
            {
                return(BitConverter.ToUInt32(valueBytes, 0));
            }
            else if (type == typeof(ulong))
            {
                return(BitConverter.ToUInt64(valueBytes, 0));
            }
            else if (type == typeof(double))
            {
                return(BitConverter.ToDouble(valueBytes, 0));
            }
            else if (type == typeof(decimal))
            {
                return(TypesInfo.BytesToDecimal(valueBytes, 0));
            }
            else if (type == typeof(float))
            {
                return(BitConverter.ToSingle(valueBytes, 0));
            }
            else if (type == typeof(char))
            {
                return(BitConverter.ToChar(valueBytes, 0));
            }
            else if (type == typeof(Guid))
            {
                return(new Guid(valueBytes));
            }
            else if (type == typeof(DateTime))
            {
                var kind  = (DateTimeKind)valueBytes[0];
                var ticks = BitConverter.ToInt64(valueBytes, 1);
                return(new DateTime(ticks, kind));
            }
            else if (type == typeof(DateTimeOffset))
            {
                var offset         = TimeSpan.FromTicks(BitConverter.ToInt64(valueBytes, 0));
                var dateTimeAsLong = BitConverter.ToInt64(valueBytes, sizeof(long));
                return(new DateTimeOffset(dateTimeAsLong, offset));
            }

            throw new NotImplementedException("Only primitive types are supported");
        }