NetDataContractSerializer 和程序集版本不匹配

发布于 2024-10-25 01:49:56 字数 418 浏览 7 评论 0原文

我正在使用 NetDataContractSerializer 在应用程序之间交换数据。我希望 ReadObject 方法在程序集版本不匹配时抛出异常。

现在,例如,当我从程序集版本 1.0.0.0 序列化我的对象,然后将其反序列化为具有相同程序集但现在版本 1.0.0.1 时,NetDataContractSerializer 愉快地吞下流并且反序列化没有问题。

当版本不匹配时是否可以中止反序列化过程?

更新:由于多种原因,我需要版本不容忍。这既是客户的要求,也是绝对确定导入处理从同一版本的应用程序导出的文件的要求。就我的应用程序而言,更改版本会使之前的导入过程变得无用,因为版本更改或多或少涉及内部结构更改。

I'm using NetDataContractSerializer to exchange data among applications. I would like the ReadObject method throw an exception when assembly versions don't match.

Now, for example, when I serialized my object from assembly version 1.0.0.0 and later deserialize it having same assembly but now version 1.0.0.1, the NetDataContractSerializer happily swallows the stream and deserializes without problems.

Is it possible to abort the deserialization process when versions don't match?

UPDATE: I need version intolerance because of many reasons. This is both a requirement from a customer, as well as a requirement to be absolutely certain that import processes the file that was exported from the same version of the application. In case of my application, changing version makes useless the previous import process, because version change equals to more or less involved internal structure change.

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

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

发布评论

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

评论(1

乖乖哒 2024-11-01 01:49:56

丑陋但有效(最好是在你的根对象上):

[DataMember]
private string AppVersion {
    get { return CurrentAppVersion; }
    set {
        if(value != CurrentAppVersion) throw new InvalidOperationException(
            "Data from version " + value + " is not compatible");
    }
}
private const string CurrentAppVersion = "1.0.11a";

Ugly but effective (ideally on your root object):

[DataMember]
private string AppVersion {
    get { return CurrentAppVersion; }
    set {
        if(value != CurrentAppVersion) throw new InvalidOperationException(
            "Data from version " + value + " is not compatible");
    }
}
private const string CurrentAppVersion = "1.0.11a";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文