从BinaryFormatter的可选字段反序列化 [英] Deserialization of optional fields from BinaryFormatter

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

问题描述

我使用该数据序列化的BinaryFormatter 的应用程序。成员已添加到从一个版本到下一个序列化不改变类名的类。 code加入到处理可能不存在于旧序列文件新增的构件的

I have an application that serializes data using BinaryFormatter. A member was added to the class that was serialized from one version to the next without changing the class name. Code was added to handle the possible absence of the added member in old serialized files:

private void readData(FileStream fs, SymmetricAlgorithm dataKey)
{
    CryptoStream cs = null;

    try
    {
        cs = new CryptoStream(fs, dataKey.CreateDecryptor(),
            CryptoStreamMode.Read);
        BinaryFormatter bf = new BinaryFormatter();

        string string1 = (string)bf.Deserialize(cs);
        // do stuff with string1

        bool bool1 = (bool)bf.Deserialize(cs);
        // do stuff with bool1

        ushort ushort1 = (ushort)bf.Deserialize(cs);
        // do stuff with ushort1

        // etc. etc. ...

        // this field was added later, so it may not be present
        // in the serialized binary data.  Check for it, and if
        // it's not there, do some default behavior

        NewStuffIncludedRecently newStuff = null;

        try
        {
            newStuff = (NewStuffIncludedRecently)bf.Deserialize(cs);
        }
        catch
        {
            newStuff = null;
        }

        _newStuff = newStuff != null ?
                new NewStuffIncludedRecently(newStuff) :
                new NewStuffIncludedRecently();
    }
    catch (Exception e)
    {
        // ...
    }
    finally
    {
        // ...
    }
}

我在现在的问题是,我真的很想刚刚冲洗和重复与其他成员,我想补充,这将意味着我会添加其他领域的try-catch类似于块 NewStuffIncludedRecently

我原以为只是让全班 [Serializable接口] ,但不会与旧的串行数据破坏兼容性?

I had thought of just making the entire class [Serializable] but wouldn't that break compatibility with the old serialized data?

我最关心的是,我并不清楚如何反序列化工作。如果我添加在处理另一个可选字段类似于以上,将它的工作?有什么其他的选择,我有处理这些变化好?

My main concern is that I'm not clear how the deserialization works. If I add in handling for another optional field similarly to above, will it work? What are other options I have for handling these changes better?

在此先感谢一如既往。

Thanks in advance as always.

推荐答案

如果您标记与 [OptionalField] 它的应该的工作的新领域,但我听说片状的一些案件的报告。我不能肯定地说,因为我避免BinaryFormatter的,因为它的版本时,有这么多的问题:)(加,这是不是从紧的一些替代品,如果你想要去的跨平台有严重问题,或CF / SL等)

If you mark the new fields with [OptionalField] it should work, but I have heard reports of flakiness in some cases. I can't say for sure, since I avoid BinaryFormatter, because it has so many issues when versioning :) (plus, it isn't as "tight" as some alternatives, and has severe issues if you want to go cross-platform, or to CF/SL etc)

如果您正在实施 ISerializable的,你可以试试:

If you are implementing ISerializable, you might try:

foreach(SerializationEntry entry in info) {
    switch(entry.Name) {
         case "Name": Name = (string)info.Value;
         case "Id": Id = (int)info.Value;
         ...
    }
}

但同样,必须强调 - 这是做事情困难的方式:P 通过这种方法,你只处理,实际上是存在的数据。

But again, must stress - this is doing things the hard way :p With this approach, you only process the data that is actually there.

这篇关于从BinaryFormatter的可选字段反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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