序列化和反序列化多个对象 [英] Serializing and Deserializing Multiple Objects

查看:249
本文介绍了序列化和反序列化多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个错误日志记录库,该库在指定的位置创建一个文件,然后将错误信息保存在该文件中,然后可由打算与该库一起编写的单独的tasker程序读取该信息。 / p>

保存方式为二进制格式(使用 BinaryFormatter() FileStream()),在这里我将名为ErrorEntry的类序列化到文件中,并将其附加到文件末尾。这意味着我背对背拥有一系列ErrorEntries。我没有将其作为列表,因为从文件中将所有信息读入列表,添加条目,然后将其保存回文件并转储资源似乎并不现实。似乎只需要执行一个写命令,然后将每个命令存储为自己的序列化对象,就会看起来更加有效。



我的问题是我可以找到有关序列化列表或单个对象的几个示例,但是对于多个对象(但单独的对象)则没有任何示例。尽管我想将它们另存为单个对象,但是在加载它们时,我想读取该对象并将该系列作为该对象的列表返回。我想象有类似的东西

  while(!EOF)
{
//反序列化每个object
//将单个对象添加到列表
}
//返回对象列表

如何从单个文件反序列化每个对象?是否有一个内部标记可以正常工作?我只需要检测它是否是文件结尾,还是需要做一些其他事情来读取它们?

解决方案

使用 FileStream fs; ,您可以执行 fs.Position!= fs.Length 检测EOF。



附加时,使用 FileMode.Append fs。 Seek(0,SeekOrigin.End); 在写入新条目之前结束。



完全反序列化:

 公共静态ICollection< T> DeserializeList< T>(FileStream fs)
{
BinaryFormatter bf = new BinaryFormatter();
List< T> list = new List< T>();
while(fs.Position!= fs.Length)
{
//反序列化文件中的每个对象
var deserialized =(T)bf.Deserialize(fs);
//将单个对象添加到列表
list.Add(反序列化);
}
//返回对象列表
返回列表;
}


I am making an error logging library which creates a file in a specified location, then saves the error information in that file, which can then be read by a separate tasker program that I intend to write in conjunction with this library.

The way that I'm saving it is in Binary Format (using BinaryFormatter() and FileStream()), where I serialize a class called ErrorEntry into the file and append it to the end of the file. What this means is that I have a series of ErrorEntries back to back. I don't have it as a list because it doesn't seem practicle to have to read all the information into a list from the file, add an entry, then save it back to the file and dump the resources. It seems much more effective to only have to do a single write command, and just store each one as its own object that is serialized.

My problem is that I can find several examples on serializing lists or individual objects, but nothing on multiple - but separate - objects. Although I want to save them as individual objects, when I load them I want to read the object and return the series as a list of that object. I imagine that there is something similar to

while(!EOF)
{
     //deserialize each object
     //add individual object to a list
}
//return the list of objects

How would I go about deserializing each object from a single file? Does it have an internal marker that the above works and I simply need to detect whether it's the end of the file, or do I need to do something else to read them?

解决方案

With FileStream fs; you could do fs.Position!=fs.Length to detect EOF.

When appending, use FileMode.Append or fs.Seek(0, SeekOrigin.End); to go to the end before writting the new entry.

Deserializing in full:

public static ICollection<T> DeserializeList<T>(FileStream fs)
{
    BinaryFormatter bf = new BinaryFormatter();
    List<T> list = new List<T>();
    while(fs.Position!=fs.Length)
    {
         //deserialize each object in the file
         var deserialized = (T)bf.Deserialize(fs); 
         //add individual object to a list
         list.Add(deserialized);
    }
    //return the list of objects
    return list;
}

这篇关于序列化和反序列化多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆