可以强命名的原因问题,在C#对象序列化? [英] Can strong naming cause problems with object serialization in C#?

查看:117
本文介绍了可以强命名的原因问题,在C#对象序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我序列化了某些配置对象,并存储在数据库中的结果字节。

I serialize some configuration objects and store the result bytes within a database.

new BinaryFormatter().Serialize(memoryStream, instance);
Convert.ToBase64String(memoryStream.ToArray());



这些对象将在以后反序列化。

These objects will be deserialized later.

new BinaryFormatter().Deserialize(memoryStream);



这是可能的,该应用程序已在反序列化时一些新的程序集版本。一般来说它工作得很好,但有时我得到一个文件负载例外:找到的程序集清单定义不匹配的程序集引用
的。大会工作的所有具有很强的命名,可以是这样的问题,我怎么能避免这个问题?

It's possible, that the Application has some new assembly versions at the time of deserialization. In general it works well, but sometimes I get a file load exception: "The located assembly's manifest definition does not match the assembly reference.". The assemblies work all with strong naming, can that be the problem and how could I avoid this problem?

感谢您的帮助。

推荐答案

当然,使用的BinaryFormatter 与数据库(即长期)存储是一个坏主意; 的BinaryFormatter 两个三大缺陷(默认):

Absolutely, using BinaryFormatter with database (i.e. long-term) storage is a bad idea; BinaryFormatter has two three big faults (by default):


  • 它包括元数据类型(胡说,如果你移动/重命名你的类型......这可能意味着强名称/版本太)

  • 它包含字段名(字段是私有的细节!)

  • 这是.NET具体的(这是一个痛苦,如果你想用其他东西)

博客在这里发表提出了与此两个具体问题 - 混淆,并自动实现的属性......我在这里就不重复文本,但你会发现它很有趣。

My blog post here raises two specific issues with this - obfuscation and automatically implemented properties... I won't repeat the text here, but you may find it interesting.

我推荐使用合同根据序列化。 的XmlSerializer 的DataContractSerializer 就足够了。如果你想少高效的二进制,然后 protobuf网可能会感兴趣。不像的BinaryFormatter ,从这个二进制文件的实施,可扩展(新场)之间移植,等它是的更快,更小,也

I recommend the use of a contract based serialization. XmlSerializer or DataContractSerializer would suffice normally. If you want small efficient binary, then protobuf-net might be of interest. Unlike BinaryFormatter, the binary from this is portable between implementations, extensible (for new fields), etc. And it is quicker and smaller, too.

这篇关于可以强命名的原因问题,在C#对象序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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