哈希表可序列化吗? [英] Are Hashtables Serializable?

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

问题描述

我看到了普遍的 Hashtable 类不可序列化;但是,我找不到任何支持该概念的现代文档.

I see a pervasive belief (2009 article) throughout the internet that the Hashtable class is not serializable; however, I cannot find any modern documentation that supports this notion.

该信念源于另一个证据不足的信念,即

The belief stems from another ill-documented belief that the IDictionary interface prevents serialization; however, I cannot find anything in MSDN that supports this claim, today.

此外, Hashtable 实现了 ISerializable ,并包含接受序列化信息的扩展方法.

Further, Hashtable implements ISerializable and contains extension methods that accept serialization information.

那是怎么回事? Hashtable 是否可序列化?围绕 IDictionary 的支持该概念的文档在哪里?

So, what's the deal? Is Hashtable serializable? Where is the documentation that supports this notion surrounding IDictionary?

进一步澄清(请阅读):

大量文档支持 IDictionary 不可序列化的说法;但是,这着重于与类的基于XML的序列化交互的使用.正如下面的注释中以及通过MSDN提到的, ISerializable 表示一个类是可序列化的.这也意味着该类必须负责自己的序列化.

The statement that IDictionary is not serializable is supported by plenty of documentation; however, this focuses on the use of XML-based serialization interactions with a class. ISerializable as mentioned both in the comments, below, and through MSDN indicates that a class is serializable. It also means the class the must be responsible for its own serialization.

我认为这否定了哈希表不可序列化的说法.这也许就是我提出这个问题的原因.

I think this negates the statement that a Hashtable is not serializable. That is perhaps the genesis of my question.

推荐答案

普遍存在的信念之所以如此普遍,是因为它是真的:

The pervasive belief is so pervasive because it's true:

var t = new Hashtable();
t.Add("Hi!", "I'm here");
t.Add("Hm", "Yup");

var serializer = new XmlSerializer(typeof(Hashtable));

using (var sw = new StringWriter())
{
  serializer.Serialize(sw, t);

  Console.WriteLine(sw.ToString());
}

投掷

NotSupportedException:不支持类型System.Collections.Hashtable,因为它实现IDictionary.

NotSupportedException: The type System.Collections.Hashtable is not supported because it implements IDictionary.

这并不意味着序列化哈希表实际上是不可能的.当然,我可以遍历所有键和值,将它们写入字符串,然后从中重建哈希表.只是我不能完全使用序列化基础结构.

That doesn't mean that it's literally impossible to serialize a hash table. Of course I can just iterate over all the keys and values, write them to a string and then reconstruct the hashtable from that. It's just that I can't use the serialization infrastructure fully.

这是什么原因?实际上非常简单-XmlSerializer旨在产生良好的XML,本着设计XML交换格式的精神.而且XML没有任何合适的字典或键值"机制.因此,为了支持哈希表序列化,他们必须使用自己的规则制作自己的子格式".在设计.NET时,这是一个巨大的禁止-XML是一种 interchange 格式.格式的任何扩展(hah)意味着无论您的想法多么出色,您都不再兼容.

What's the reasoning here? It's actually quite simple - XmlSerializer is designed to produce good XML, in the spirit of the interchange format XML was designed to be. And XML doesn't have any kind of dictionary or "key-value" mechanism that would fit. So to support hashtable serialization, they'd have to make their own "sub-format" with its own rules. And back when .NET was being designed, this was a huge no-no - XML was an interchange format. Any extension (hah) to the format means you're no longer compatible, no matter how good of an idea you have.

当然,如今,每个人及其祖母都在生成XML数据,这些数据不用于交换目的.这并不是一件坏事(毕竟,.NET config 文件也是XML格式).但这也是一种错误.

Of course, nowadays, everyone and their grandmother are producing XML data that isn't used for interchange purposes. And it's not entirely a bad thing (after all, .NET config files are also a XML format). But it's also kind of wrong.

相反,采用类似 BinaryFormatter 之类的东西.这是.NET团队设计整个格式的类,并且不受标准限制.瞧瞧- BinaryFormatter 可以很好地对 Hashtable 进行序列化和反序列化.

In contrast, take something like BinaryFormatter. That's a class where the .NET team designed the whole format, and isn't limited by a standard. And lo and behold - BinaryFormatter can serialize and deserialize a Hashtable just fine.

因此,稍微正确一点的信念是无法将Hashtable序列化为有效的标准XML.特别是XmlSerializer类在尝试序列化Hashtable时将引发错误."

So the slightly more correct belief would be "Hashtable cannot be serialized to valid standard XML. The XmlSerializer class in particular will throw an error when you attempt to serialize a Hashtable."

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

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