2

For example, i have:

struct SomeStruct
{
   //some fields
   //each instance will store info read from file, maybe be 3kb, maybe more.
}

List<SomeStruct> lst = new List<SomeStruct>();

I will add to that list crazy amount of objects, so it will end up to 10Gbs or more in size. Can i serialize lst without any errors like out of memory and etc? Can i deserialize it later?

4

1 回答 1

1

如果您可以一次将项目列表保存在内存中,那么您应该有很大的机会对它们进行序列化/反序列化。您可能希望在流中单独处理它们,而不是一次序列化/反序列化整个列表。这将处理您可能遇到的任何边缘情况。

伪代码:

private void SerializeObjects(List<foo> foos, Stream stream)
{
    foreach (var f in foos)
    {
        stream.Write(f);
    }
}

private void DeserializeObjects(List<foo> foos, Stream stream)
{
    foo f = stream.ReadFoo();
    while (f != null)
    {
        foos.Add(f);
        f = stream.ReadFoo();
    }
}
于 2012-10-31T13:48:38.683 回答