如何序列化到现有的文件? [英] How to serialize on to existing file?

查看:92
本文介绍了如何序列化到现有的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我有一个包含由BinaryFomatter序列化对象的文件。现在,我希望能够序列化另一个对象,并追加该上现有的文件。

Let say I have a file that contains a serialized object by BinaryFomatter. Now I want to be able to serialize another object and APPEND this on that existing file.

我该怎么办呢?

推荐答案

首先,如果你真正需要的是存储对象的数组 - 把它们放在一个容器和序列容器,正如前面所说的

First, if what you really need is to store an array of object - put them in a container and serialize the container, as said earlier.

现在,如果你真的想存储两个序列化对象级联单个文件:

Now, if you really want to store two serialized object in a single file concatenated:

我不知道这是可能的开箱即用,但你可以做两件事情。

I'm not sure that this is possible "out of the box", but you can do two things.


  1. 使用,或创造你自己的半tar文件格式,在其中,你会写一个描述文件的内容的文件的开头小头(或结束)(#对象和大小)。

  1. Use, or invent your own semi-tar file format, in which you'll write a small header in the beginning (or end) of the file that describes the content of the file (#'objects, and sizes).

写C#代码来检测每个序列化对象自己的开头和结尾。我不能完全肯定这是可能的。 这里是序列化格式的非官方的文档。它包含了头,但谁也不能保证,这头将不会出现在文件中。

Write a C# code to detect the beginning and end of each serialized object yourself. I'm not entirely sure it's possible. Here is a non-official documentation of the serialization format. It contains header, however there's no guarantee that this header won't appear in the file.

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

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