如何反序列化通用列表?

How to serialise generic list?

我有一个 employeessupervisors

的通用列表
List<Employee> employees = new List<Employee>();
List<Supervisor> supervisors = new List<Supervisor>(); 

它们都是我标记为 [Serializable]

employee class 的一部分

我可以把这两个列表放到另一个列表中以便序列化更容易吗?

我看过序列化教程,但它们只指定了一个或更少的通用列表。

我提供了一个模板,我想点击一个'Save'按钮并完成序列化过程,最好将两个列表合并为一个更大的列表。

private void btnSave_Click(object sender, EventArgs e)
{
     FileStream outFile;
     BinaryFormatter bFormatter = new BinaryFormatter();
}

您可以将此列表放在另一个 class 容器中并 serialize/deserialize 它。

[Serializable]
public class Container 
{
     public List<Employee> employees = new List<Employee>();
     public List<Supervisor> supervisors = new List<Supervisor>();
} 

您实际上可以将两个列表序列化为一个流:

using (FileStream fs = new FileStream(..., FileMode.OpenOrCreate)) {
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(fs, employees);
    bf.Serialize(fs, supervisors);
}

BinaryFormatter 为数据块添加元数据前缀,这显然也允许它将数据连接到一个文件中。为了读回 supervisors 你必须先加载另一个列表。由于列表不是固定长度的记录,因此当您重写第一个列表时,第二个列表可能会变得不可用,并且要么在孤立数据后面丢失它,要么覆盖其中的一部分。

因为有太多东西可以阻止它工作,所以很容易为倍数创建一个 "holder" 或容器 class:

[Serializable()]
public class BFHolder
{
    public List<Employee> employees { get; set; }
    public List<Supervisor> supervisors { get; set; }   
    public BFHolder()
    {
    }
}

反序列化后,您可以根据需要提取列表。

还有一个——更好的——选项是ProtoBuf-Net(向下滚动到阅读我)。除了比 BinaryFormatter 更快和创建更小的输出之外,它还包括将多个对象序列化为一个流的方法。

using (FileStream fs = new FileStream(..., FileMode.OpenOrCreate)) {
    Serializer.SerializeWithLengthPrefix<List<Employee>>(fs, employees,
                    PrefixStyle.Base128);
    Serializer.SerializeWithLengthPrefix<List<Supervisor>>(fs, supervisors,
                    PrefixStyle.Base128);
}