将各种类型序列化到一个文件中

发布于 2024-09-14 01:49:38 字数 319 浏览 3 评论 0 原文

我正在使用 BinaryFormatter 来序列化我的对象。我的相关列表中有各种对象类型。是否有一种“最佳”方法将所有对象序列化到一个文件中,但能够在反序列化时将它们分开?目前,我正在反序列化整个文件并检查每个对象中的特定字段,以查看反序列化后应将它们重新添加到哪个列表。例如,我所有的福特汽车都有“FORD”字段,丰田汽车有“TOYOTA”等。反序列化后,我“foreach”对象,检查它们的“company”字段,然后将它们分配给 List ;

我不确定这是否是最有效的做事方式。有什么建议吗? 感谢您的帮助

I'm using a BinaryFormatter to serialize my object. I have various object types in their relevant lists. Is there a 'best' way to serialize all the objects into one file, but be able to separate them on deserialization? At the moment, I'm deserializing the whole file and checking a specific field in each object to see what list I should re-add them to after deserialization. For example all my Ford cars have the 'FORD' field, Toyota have 'TOYOTA' etc.. Upon deserialization, I 'foreach' through the objects, check their 'company' field and then assign them to a List<Company>.

I'm not sure if this is the most efficient way of doing things. Any suggestions?
Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-09-21 01:49:38

这很简单......而不是序列化每个单独的项目。只需创建一个“父”类,它只具有每个项目类型的属性。然后您可以序列化该一个对象。当您在另一端反序列化它时,它已经为您完全整理好了。

This one's easy ... instead of serializing each individual item. Just make a "parent" class which simply has a property for each item type. And then you can serialize that one object. When you deserialize it on the other end it will already be full sorted out for you.

能否归途做我良人 2024-09-21 01:49:38

您可以更有效地执行此操作的唯一方法是将它们序列化为不同的文件(基于对您的域有意义的字段标准)。因为您不能保证它们将以特定顺序序列化到文件中,所以当您反序列化时,您必须将整个文件读入内存,以查找字段值是您所在类型的所有实例寻找。

我认为您已经在序列化到文件的限制范围内尽了最大努力。您是否考虑过将 SQLite 作为您的存储机制?

The only way that you could do this more efficiently is to serialize them into different files (based on a field criteria that makes sense to your domain). Because you are not guaranteed that they are going to be serialized into the file in a specific order, you'll have to read the entire file into memory when you deserialize anyways to find all instances of the type where the field value is what you are looking for.

I think you're already doing the best that you will within the constraints for serializing to a file. Have you considered SQLite to be your storage mechanism?

以可爱出名 2024-09-21 01:49:38

你正在做的事情听起来像一本字典(Dictionary>)。不过,请在您的问题中提供一些源代码。

没有内置的方法来序列化字典,但您可以创建自己的自定义类。我将把它作为练习留给你去谷歌搜索:)

如果你想继续以相同的方式序列化,但由对象内部的已知成员分隔(并且该成员始终相同),你可以使用使用简短代码提取对象的 LINQ 查询:

IEnumerable<Company> companiesFromFile = GetDeserializedFile();
var dictionaryFromFile = companiesFromFile.ToDictionary(c => c.Name);

然后,如果必须分隔列表,可以通过以下方式完成:

List<Company> toyotaList = dictionaryFromFile["Toyota"];
// etc

What you are doing sounds like a dictionary (Dictionary<string, List<Company>>). Please provide some source code in your question, though.

There is no built-in way to serialize a dictionary, but you can make your own custom class. I'll leave that as an exercise to you to google that :)

If you want to continue to serialize the same way, but separate by a known member that is inside the objects (and the member is always the same), you can use a LINQ query to extract the objects with short code:

IEnumerable<Company> companiesFromFile = GetDeserializedFile();
var dictionaryFromFile = companiesFromFile.ToDictionary(c => c.Name);

Then, if you have to separate the lists, you can do it via:

List<Company> toyotaList = dictionaryFromFile["Toyota"];
// etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文