新版本与序列化 [英] New versions vs. serialization

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

问题描述




我正在编写一个程序,该程序将继续不断扩展

,其功能需要信息。到目前为止,已经使用记事本在txt文件中找到并修改了此

信息的来源,该文件被解析为对象中的字段。 (这个解决方案,因为这是一个较旧的fortran程序正在使用的格式

,我们需要以这种方式在

过渡期内这样做)。现在我希望能够从程序中保存并且

希望切换到对象序列化,但是如果我的类被序列化的话,我很担心后面的
兼容性在磁盘上不断延长

.


如果我扩展一个带有字段的类并尝试从

中取消此类没有该字段的源码,我想我会得到一个例外,对吧?

是否有任何属性可以为

提供某种默认值字段或表示它''OK!''找不到该字段。


一个类必须与字段完全匹配。


或者我会使用xml并编写我自己的序列化和

反序列化方法。相当麻烦,但如果我想要这个

自由度可能是必要的。


另一种方法可以是保留旧框架然后解析信息

从这个旧框架到新框架 - 就像你在单词2003中打开一个单词97

文件。


请评论。


最好的问候丹麦Jesper。

Hi,

I''m writing a program which is, and will continue to be, constantly extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this way in
a transition period). Now I would like be able to save from the program and
wish to switch to object serialization, but I''m concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I''ll get an exception, Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is ''OK!'' that the field wasn''t found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.

推荐答案

你有没有找到解决这个问题的方法? ?我有一个类似的问题,并且

正在寻找解决方案。

-

Steve

" Jesper" ;写道:
Did you ever find a solution to this problem? I have a similar issue and am
looking for a solution.
--
Steve
"Jesper" wrote:


我正在编写一个程序,该程序现在并将继续不断扩展,具有所需的功能信息。到目前为止,已经在txt文件中使用记事本找到并修改了此信息的来源,该文件被解析为对象中的字段。 (这个解决方案,因为这是旧的fortran程序使用的格式,我们需要在过渡期间以这种方式完成)。现在我希望能够从程序中保存并希望切换到对象序列化,但如果我在磁盘上序列化的类经常是
,我会关注后向兼容性。扩展。

如果我用一个字段扩展一个类并尝试从没有该字段的
源中取消该类,我想我会得到一个例外,对吧? 。
是否有任何属性可以为
字段提供某种默认值,或者表明它已经''OK!''找不到该字段。
一个类必须与字段完全匹配。

或者我会使用xml并编写自己的序列化和反序列化方法。相当麻烦,但如果我想要这个自由度可能是必要的。

另一种方法可以是保留旧框架,然后从旧框架解析信息到新框架 - 比如当你在单词2003中打开一个单词97
时。请评论。

最好的问候丹麦Jesper。
Hi,

I''m writing a program which is, and will continue to be, constantly extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this way in
a transition period). Now I would like be able to save from the program and
wish to switch to object serialization, but I''m concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I''ll get an exception, Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is ''OK!'' that the field wasn''t found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.



不知道这是否解决了您的问题,但您可以使用实现IDictionary接口的类之一

,并且可以序列化。键将

为表示所命名字段的字符串。这些值将是对象

,当你从字典中取出它们时,你必须将它们强制转换为正确的类型。如果找不到某个字段,则可以使用默认的

值添加(并在写出字典时保存)。但是,以这种方式通过字典访问字段

的CPU时间相当昂贵。


如果您不想支付访问该字段的费用"字段"从

字典开始,每次需要它们时,你都可以为你的
可扩展类创建一个构造函数。构造函数将从文件中读取序列化字典

。对于类中的每个字段,它将尝试从字典中获取它b / b
;如果发现它会将它从字典中分配给对象中的

字段。如果没有找到,它会适当地初始化它。

构造之后,只能从对象访问字段。


当你的可扩展类发生变化时,你必须重新编译新类

定义并更改构造函数。但是,旧的序列化的

字典将被读取,并且该类的新版本用字典中的现有字段初始化为

。你写的新构造函数代码会在/

字典中找不到新字段时默认




你的应用程序的某些部分会写出可序列化的字典,

从你使用的对象更新字典中的键/值。


这与保存XML并解析它以构建

可扩展类的对象大致相同,但使用字典可以省去

编写解析逻辑。


你可以为每个可扩展的类提供单独的字典和文件,

但它可能更容易一个可序列化的字典。您可以轻松地使用该类限定字段名称(字符串),如foo.in,

" bar.in",并将它们用于建立酒吧和foo适当的对象。


Steve Teeples <圣********** @ discussions.microsoft.com>在消息中写道

news:55 ********************************** @ microsof t.com ...
Don''t know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
你有没有找到解决这个问题的方法?我有类似的问题,并且正在寻找解决方案。
-
史蒂夫

" Jesper"写道:
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:


我正在编写一个程序,这个程序将会并将继续不断扩展
具有需要信息的功能。到目前为止,已经使用记事本在txt文件中找到并修改了此信息的来源,该文件已被解析为对象中的字段。 (这个解决方案,因为这是旧的fortran程序正在使用的格式,我们需要在过渡期间以这种方式进行)。现在我希望能够从程序中保存

希望切换到对象序列化,但是如果我在磁盘上序列化的类是不断的,我会关注后向兼容性
扩展。

如果我扩展一个带有字段的类并尝试从没有该字段的
源中取消该类,我想我会得到一个例外,
对吧?
是否有任何属性可以为
字段提供某种默认值,或者表明它是''OK!''该字段未找到。

一个类必须与字段完全匹配。

或者我会使用xml并编写自己的序列化和反序列化方法。相当麻烦,但如果我想要这个
自由度可能是必要的。

另一种方法可以是保留旧框架,然后从这个旧框架解析信息到新的 - 就像当你在单词2003中打开一个单词97
时请评论。

最好的问候丹麦Jesper。
Hi,

I''m writing a program which is, and will continue to be, constantly
extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that
was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this
way in
a transition period). Now I would like be able to save from the program
and
wish to switch to object serialization, but I''m concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I''ll get an exception,
Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is ''OK!'' that the field wasn''t found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want
this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.



这是我见过的最佳解决方案。它看起来至少有一半

十几个人有类似我的问题,但我还没有看到他们的任何帖子提出的

解决方案。至于提案,它将需要时间以这种方式重新编码,但我认为它会起作用。感谢

的想法。


-

史蒂夫

" Fred Mellender"写道:
This is the best solution I''ve seen proposed yet. It appears at least a half
a dozen other people have a question similar to mine but I''ve not yet seen a
solution proposed to any of their posts. As for the proposal, it will
require time to re-code it this way, but I think it would work. Thanks for
the idea.

--
Steve
"Fred Mellender" wrote:
不知道这是否解决了您的问题,但您可以使用其中一个实现IDictionary接口的类,并且可以序列化。键将是表示命名字段的字符串。这些值将是从
字典中拉出它们时必须转换为正确类型的对象。如果找不到某个字段,则可以添加一个默认的值(并在写出字典时保存)。但是,以这种方式通过字典访问字段在CPU时间上相当昂贵。

如果您不想支付访问字段的费用。从
字典开始,每次需要它们时,你都可以为你的
可扩展类创建一个构造函数。构造函数将从文件中读取序列化字典。对于类中的每个字段,它将尝试从字典中获取它;如果找到它会将它从字典中分配给对象中的
字段。如果没有找到,它会适当地初始化它。
构建后,只能从对象访问字段。

当您的可扩展类更改时,您必须重新编译新类
定义并更改构造函数。但是,将读取旧的序列化
字典,并使用字典中的现有字段初始化类的新版本。新字段将被您编写的新构造函数代码默认,如果/如果它们未在
字典中找到。

应用程序的某些部分会写出可序列化的字典,
从您使用的对象更新字典中的键/值后。

这与保存XML并解析它以构建对象大致相同
可扩展类,但使用字典可以省去编写解析逻辑的麻烦。

每个可扩展类都可以有单独的字典和文件,<但是,拥有一个可序列化的字典可能更容易。您可以轻松地使用类来限定字段名称(字符串),如foo.in,
bar.in,并使用它们来构建bar。和foo适当的对象。

Steve Teeples <圣********** @ discussions.microsoft.com>在消息中写道
新闻:55 ********************************** @ microsof t.com。 ..
Don''t know if this solves your problem, but you could use one of the classes
that implements IDictionary interface, and is serializable. The keys would
be strings that represented the field named. The values would be objects
that you would have to cast to the correct type when you pulled them from
the dictionary. If a field was not found it could be added with a default
value (and saved when the dictionary was written out). Accessing a field
through a dictionary in this way is rather expensive in CPU time, though.

If you did not want to pay the cost to access the "fields" from the
dictionary, each time they are needed, you could make a constructor for your
extensible class(es). The constructor would read the serialized dictionary
from the file. For each field in the class, it would attempt to fetch it
from the dictionary; if found it would assign it from the dictionary to the
field in the object. If not found, it would initialize it appropriately.
After construction, access to the fields is just from the object.

When your extensible class changes, you have to recompile the new class
definition and change the constructor. However, the old serialized
dictionary will be read and the new version of the class initialized with
the existing fields from the dictionary. The new fields would be defaulted
by the new constructor code you wrote, when/if they were not found in the
dictionary.

Some part of your application would write out the serializable dictionary,
after updating the key/values in the dictionary from the object you were
using.

This is about the same as saving XML and parsing it to build an object of
"extensible class", but use of the dictionary saves you the trouble of
writing parsing logic.

You could have separate dictionaries and files for each extensible class,
but it might be easier to have one serializable dictionary. You could
easily qualify the field names (strings) with the class, as in "foo.in",
"bar.in", and use them to build the "bar" and "foo" objects appropriately.

"Steve Teeples" <St**********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
你有没有找到解决这个问题的方法?我有类似的问题,并且正在寻找解决方案。
-
史蒂夫

" Jesper"写道:
Did you ever find a solution to this problem? I have a similar issue and
am
looking for a solution.
--
Steve
"Jesper" wrote:


我正在编写一个程序,这个程序将会并将继续不断扩展
具有需要信息的功能。到目前为止,已经使用记事本在txt文件中找到并修改了此信息的来源,该文件已被解析为对象中的字段。 (这个解决方案,因为这是旧的fortran程序正在使用的格式,我们需要在过渡期间以这种方式进行)。现在我希望能够从程序中保存

希望切换到对象序列化,但是如果我在磁盘上序列化的类是不断的,我会关注后向兼容性
扩展。

如果我扩展一个带有字段的类并尝试从没有该字段的
源中取消该类,我想我会得到一个例外,
对吧?
是否有任何属性可以为
字段提供某种默认值,或者表明它是''OK!''该字段未找到。

一个类必须与字段完全匹配。

或者我会使用xml并编写自己的序列化和反序列化方法。相当麻烦,但如果我想要这个
自由度可能是必要的。

另一种方法可以是保留旧框架,然后从这个旧框架解析信息到新的 - 就像当你在单词2003中打开一个单词97
时请评论。

最好的问候丹麦Jesper。
Hi,

I''m writing a program which is, and will continue to be, constantly
extended
with functionality that require information. Until now the source of this
information has been found and modified using notepad in a txt file that
was
parsed into fields in objects. (This solution because this was the format
that an older fortran program was using and we needed to do it in this
way in
a transition period). Now I would like be able to save from the program
and
wish to switch to object serialization, but I''m concerned about backward
compatibility if my classes that are serialized on disk is constantly
extended.

If I extend a class with a field and try to desirialize this class from a
source which did not have the field, I guess I''ll get an exception,
Right?.
Are there any attributes that can provide some kind of default values for
fields or indicate that it is ''OK!'' that the field wasn''t found.

Must a class match exactly the fields.

Alternatively I would use xml and write my own serialization and
deserialization methods. Rather cumbersome but maybe necessary if I want
this
degree of freedom.

Another way could be to keep the old framework and then parse information
from this old framework to the new one - like when you open a word 97
document in word 2003.

Please comment.

best regards Jesper, Denmark.




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

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