/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T ReadGeneric <T>() { ITypeSerializer <T> serializer = InternalTypeCache <T> .Serializer; if (serializer is IBuiltinTypeSerializer) { return(serializer.Deserialize(ref this)); } else { ObjectType type = ReadTypeFromCachedTag(); int len = ReadLengthByType(type); if (len < 0) { throw new InvalidCastException(string.Format(Resources.InvalidCastFormat, type, typeof(T).AssemblyQualifiedName)); } if (m_ReadCount + len > m_Size) { throw new StreamTooShortException(Resources.StreamTooShort); } AccelReader reader = new AccelReader(m_Source + m_ReadCount, len, m_Encoding, m_IsLittleEndian); m_ReadCount += len; return(serializer.Deserialize(ref reader)); } }
/// <summary> /// 反序列化<typeparamref name="T"/>类型对象实例 /// </summary> /// <typeparam name="T">反序列化的对象类型</typeparam> /// <param name="bytes">保存了对象数据的字节数组</param> /// <param name="index">开始读取的索引位置</param> /// <param name="length">可以读取的字节大小</param> /// <returns>反序列化的对象实例</returns> /// <exception cref="ArgumentNullException"><paramref name="bytes"/>为null</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="bytes"/>长度不足</exception> public static T Deserialize <T>(byte[] bytes, int index, int length) { //if (length < 2) //{ // return default; //} if (bytes == null) { throw new ArgumentNullException(nameof(bytes), Resources.ByteArrayIsNull); } if (bytes.Length - index < length) { throw new ArgumentOutOfRangeException(nameof(bytes), Resources.ByteArrayTooShort); } AccelReader.ReadGlobalConfig(bytes[index], out Encoding encoding, out Endian endian); fixed(byte *p = bytes) { AccelReader reader = new AccelReader(p + index + 1, length - 1, encoding, endian == Endian.LittleEndian); ITypeSerializer <T> serializer = InternalTypeCache <T> .Serializer; if (serializer is IBuiltinTypeSerializer && !reader.HasNext()) { return(default);