示例#1
0
        /// <summary>
        /// 使用指定长度从流中取回二进制形式序列化的对象. 没有解析成功, 会抛出异常.
        /// </summary>
        /// <typeparam name="T">取回对象的类型</typeparam>
        /// <param name="reader"></param>
        /// <param name="length">指定取回的对象大小.</param>
        /// <returns>返回解析到的对象.</returns>
        public static T RetrieveBinaryObject <T>(this BinaryReader reader, int length)
        {
            length.ShouldLargerThan(0);
            DebugHelper.WriteLine(string.Format("Try reading object (length: {0})...", length));
            byte[] objBytes = reader.ReadBytes(length);
            var    result   = BinarySerializationHelper.Deserialize <T>(objBytes);

            DebugHelper.WriteLine("Object retrieved.");
            return(result);
        }
示例#2
0
        /// <summary>
        /// 对象会先进行二进制序列化, 然后以二进制方式向底层的流中写入(不大于4GB).
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="writer">流</param>
        /// <param name="obj">目标对象</param>
        /// <param name="writeLengthHeader">是否写入头. 如果为true, 将首先自动写入4个字节的头, 内容是对象序列化后的长度</param>
        /// <remarks>编码采用UTF8</remarks>
        public static void WriteBinaryObject <T>(this BinaryWriter writer, T obj, bool writeLengthHeader = true)
        {
            if (obj is null)
            {
                throw new ArgumentNullException();
            }
            using MemoryStream mem = new MemoryStream();
            int len = BinarySerializationHelper.Serialize(obj, mem);

            if (writeLengthHeader)
            {
                byte[] objectLenBytes = BitConverter.GetBytes(len);
                DebugHelper.WriteLine(string.Format("Write Length {0} as 4 Bytes header!", objectLenBytes.Length));
                writer.Write(objectLenBytes); //write length as header
            }
            DebugHelper.WriteLine("Try to write object.");
            var objBytes = mem.GetBuffer();

            writer.Write(objBytes);
            DebugHelper.WriteLine("Object wrote.");
        }
示例#3
0
 /// <summary>
 /// 从fileinfo代表的文件中进行二进制反序列化
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="fileInfo"></param>
 /// <returns>反序列化后的对象</returns>
 public static T DeserializeBinaryObject <T>(this FileInfo fileInfo)
 {
     return(BinarySerializationHelper.Deserialize <T>(fileInfo.FullName));
 }
示例#4
0
 /// <summary>
 /// 把对象序列化到二进制文件中
 /// </summary>
 /// <param name="objToSave">欲保存的对象</param>
 /// <param name="filename">包含路径的文件名</param>
 public static void ToBinaryFile(this object objToSave, string filename)
 {
     BinarySerializationHelper.Serialize(objToSave, filename);
 }
示例#5
0
 /// <summary>
 /// 把对象序列化到二进制流当中
 /// </summary>
 /// <param name="objToSave">欲保存的对象(大小&lt;=4GB)</param>
 /// <param name="stream">写入的流</param>
 /// <returns>流的长度</returns>
 public static int ToBinaryStream(this object objToSave, Stream stream)
 {
     return(BinarySerializationHelper.Serialize(objToSave, stream));
 }
示例#6
0
 /// <summary>
 /// 序列化为字节数组
 /// </summary>
 /// <param name="objToSave"></param>
 /// <returns></returns>
 public static byte[] ToBytes(this object objToSave)
 {
     return(BinarySerializationHelper.Serialize(objToSave));
 }