序列化和对象版本在C#对象、版本、序列化

2023-09-03 11:14:28 作者:离愧V

如果我想序列化一个对象,我必须使用 [Serializable接口] 属性,所有的成员变量将被写入文件。我不知道该怎么怎么做的版本例如如果我添加了新的成员变量(重命名变量或只是删除一个变量),然后我打开(反序列化)的文件我怎么能确定对象/文件的版本,所以我可以正确地设置新的成员或采取某种迁移?如何确定该变量是在加载过程中初始化或没有(由解串器忽略)。

我知道有宽容版本的方法,我可以用标记变量[Opt​​ionalField(VersionAdded = 1)] 属性。如果我打开一个旧文件的框架会忽略这个可选的(新变量),这将只是零/零。但同样我怎么能确定是否变量是由加载或初始化它被忽略了。

我可以写的类/对象的版本号的流。使用 ISerializable的办法和构造函数(SerializationInfo中oInfo,的StreamingContext上下文)方法读取此版本号。这正是告诉我什么是类版本的激流。

不过,我预计,这种不同的版本已经实现在C#中的流框架。我试图获得来自大会的版本的SerializationInfo ,但它始终设置为当前版本不是对象被保存时,这是所使用的版本。

什么是preferred的方法呢?我发现了很多在网络上的文章,但我不能找到一个很好的解决方案,解决了版本...

任何帮助是AP preciated 谢谢, 深渊

解决方案

原谅我,如果一些我写的东西太明显了,

首先,请!你必须停止思考,你是序列化对象... 这是完全不正确的,这是您的对象的部分的方法不被保留。 你是坚持信息 - 等等。数据仅

.NET序列化也序列化的对象,它包含程序集名称和版本的类型名称,所以当你反序列化 - 它相比之下,将与该信息可表现的类型持久化装配信息 - 如果他们不是同时它会返回一个例外。

旁边的版本问题 - 而不是什么都可以如此轻易连载..尝试序列化一个System.Drawing.Color类型,你会开始理解问题的.NET序列化的过于简单机制

除非你打算连载有没有计划发展我不会用.NET提供的序列化机制的东西真的很简单。

获得焦点回到你的问题,你可以在这里阅读有关版本的无知能力: http://msdn.microsoft.com/en-美国/库/ ms229752(V = VS.80)的.aspx 它提供的BinaryFormatter。

您也应该检查XML序列化有一些不错的能力,但最大的好处是,你得到一个XML这是人类可读的,所以即使你有并发症,这与你的类型的版本控制你的数据永不丢失。

但最后,我建议你要么使用数据库实体框架,以保存数据或编写自己的平面文件管理器。而EF是很好的大多数解决方案,有时你可能想要的东西更轻坚持的东西很简单。 (我的暗示是,我再也看不到一个解决方案,.NET序列化可以是相关的。)

我希望这可以帮助,祝你好运。

C 序列化与反序列化

If I want to serialize an object I have to use [Serializable] attribute and all member variables will be written to the file. What I don't know how to do versioning e.g. if I add a new member variable (rename a variable or just remove a variable) and then I open (deserialize) the file how can I determine the object/file version so I can correctly set the new member or take some kind of migration? How can I determine that the variable was initialized during the load or not (ignored by deserializer).

I know that there are version tolerant approaches and I can mark variables with [OptionalField(VersionAdded = 1)] attribute. If I open an old file the framework will ignore this optional (new variable) and it will be just zero/null. But again how can I determine if the variable is initialized by load or it was ignored.

I can write the class/object version number to the stream. Use the ISerializable approach and in the constructor(SerializationInfo oInfo, StreamingContext context) method read this version number. This will exactly tell me what is the class version in the stream.

However I expected that such kind of versioning is already implemented by the streaming framework in C#. I tried to obtain the Assembly version from the SerializationInfo but it is always set to current version not to the version which was used when the object was saved.

What is the preferred approach? I found a lot of articles on the net, but I could not find a good solution for this which addresses versioning...

Any help is appreciated Thanks, Abyss

解决方案

Forgive me if some of what I write is too obvious,

First of all, please! you must stop thinking that you are serializing an object... That is simply incorrect as the methods which are part of your object are not being persisted. You are persisting information - and so.. DATA only.

.NET serialization also serializing the type name of your object which contain the assembly name and its version, so when you deserialize - it compares the persisted assembly information with the type that will be manifested with the information - if they are not the same it will return an exception.

Beside the versioning problem - not everything can be serialized so easily.. try to serialize a System.Drawing.Color type and you will begin to understand the problems with the over simplistic mechanism of .NET serialization.

Unless you plan to serialize something really simple which has no plans to evolve I wouldn't use the serialization mechanism provided by .NET.

Getting the focus back to your question, you can read here about the versioning ignorance ability: http://msdn.microsoft.com/en-us/library/ms229752(v=vs.80).aspx which is provided for BinaryFormatter.

You should also check XML Serialization which has some nice abilities, but the biggest benefit is that you getting an XML which is Human readable so your data will never be lost even if you had complication with the versioning of your types.

But finally, I recommend you either use Database with Entity Framework to persist your data or write your own flat file manager.. while EF is very good for most solutions, sometime you might want something lighter to persist something very simple. (my imply is that I can no longer see a solution where .NET serialization can be relevant.)

I hope this helps, Good luck.