序列化的HashSet [英] Serializing a HashSet

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

问题描述

我想序列化HashSet的,但我有没有运气。每当我试图打开序列化的数据,我得到一个空的HashSet。然而,一个List正常工作。例如:code:

I'm trying to serialize a Hashset but I'm having no luck. Whenever I try to open the serialized data, I get an empty HashSet. However, a List works fine. Example code:

[Serializable()]
public class MyClass : ISerializable
{
    public MyClass(SerializationInfo info, StreamingContext ctxt)
    {
        HashSet<string> hashset = (HashSet<string>)info.GetValue("hashset", typeof(HashSet<string>));
        List<string> list = (List<string>)info.GetValue("list", typeof(List<string>));
        Console.WriteLine("Printing Hashset:");
        foreach (string line in hashset)
        {
            Console.WriteLine(line);
        }
        Console.WriteLine("Printing List:");
        foreach (string line in list)
        {
            Console.WriteLine(line);
        }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        HashSet<string> hashset = new HashSet<string>();
        hashset.Add("One");
        hashset.Add("Two");
        hashset.Add("Three");
        info.AddValue("hashset", hashset);
        List<string> list = new List<string>();
        list.Add("One");
        list.Add("Two");
        list.Add("Three");
        info.AddValue("list", list);
    }
}

和运行的时候,它打印出来:

And when run, it prints out:

Printing Hashset:
Printing List:
One
Two
Three

所以列表工作正常,但HashSet的回来空。小卡 - 任何人都可以看到我在做什么错了?谢谢

So the List works fine, but the HashSet comes back empty. A little stuck - can anyone see what I'm doing wrong? Thanks

推荐答案

更新

由于汉斯帕桑特表示有简单的解决方法,只需拨打 HashSet.OnDeserialization 手动。

As Hans Passant stated there are simple workaround, just call HashSet.OnDeserialization manually.

var hashset = (HashSet<string>)info.GetValue("hashset", typeof(HashSet<string>));
hashset.OnDeserialization(this);

这也有助于与其他泛型集合。

It also helps with other Generic collections.

据我可以看到这可能是在 HashSet的&LT的bug; T&GT; 的实施。 的HashSet 正确序列化到的SerializationInfo

As far as I can see this is probably bug in HashSet<T> implementation. HashSet correctly serialized into SerializationInfo:

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
  if (info == null)
  {
    throw new ArgumentNullException("info");
  }
  info.AddValue("Version", this.m_version);
  info.AddValue("Comparer", this.m_comparer, typeof(IEqualityComparer<T>));
  info.AddValue("Capacity", (this.m_buckets == null) ? 0 : this.m_buckets.Length);
  if (this.m_buckets != null)
  {
    T[] array = new T[this.m_count];
    this.CopyTo(array);
    info.AddValue("Elements", array, typeof(T[]));
  }
}

的SerializationInfo 正确还原。您还可以检查自己,看看到: <$c$c>(((System.Collections.Generic.HashSet<string>)(info.m_data[0]))).m_siInfo.m_data[3]但无法恢复其状态:

and SerializationInfo correctly restored. You can check also by yourself, take a look to: (((System.Collections.Generic.HashSet<string>)(info.m_data[0]))).m_siInfo.m_data[3] but fails to restore its state:

它所要做的只是卖场的SerializationInfo

protected HashSet(SerializationInfo info, StreamingContext context)
{
  this.m_siInfo = info;
}

您可以检查(HashSet的).m_siInfo.MemberValues​​ [3] ,价值被正确地将被格式化恢复,但不是跨preTED由的HashSet

You can check (hashset).m_siInfo.MemberValues[3], values was correcly restored by formatter but not "interpreted" by HashSet.

类似的问题有词典&LT; TKEY的,TValue&GT; 或如的LinkedList&LT; T&GT;

名单,其中,T&GT; (或类似的基于阵列的集合,如堆栈&LT; T&GT; )的,因为没有任何问题他们序列化为阵列(无需特殊的逻辑)。

List<T> (or similar array based collections such as Stack<T>) has no problem since they serialized as array (without special logic).

解决方法张贴由Hans帕桑特。

Workaround was posted by Hans Passant.

恕我直言,的BinaryFormatter 是不是真的来存储值良好和有效的方式。你可以尝试使用<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer%28VS.95%29.aspx"相对=nofollow> DataContractSerializer的(它可以处理这种类型)或序列助手去如protobuf.net,json.net等见<一href="http://stackoverflow.com/questions/3904494/why-is-binary-serialization-faster-than-xml-serialization/3904761#3904761">Why是二进制序列比XML序列化的速度快?和<一href="http://stackoverflow.com/questions/3790728/performance-tests-of-serializations-used-by-wcf-bindings/3793091#3793091">Performance序列化的使用WCF绑定测试

IMHO, BinaryFormatter is not really good and efficient way to store values. You can try to use DataContractSerializer (it can handle such types) or go with serialization helpers such as protobuf.net, json.net etc. See Why is binary serialization faster than xml serialization? and Performance Tests of Serializations used by WCF Bindings

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

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