我正在尝试序列化一个哈希集,但我没有运气。每当我尝试打开序列化数据时,我都会得到一个空的 HashSet。但是,列表工作正常。示例代码:
[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);
}
}
运行时打印出:
Printing Hashset:
Printing List:
One
Two
Three
因此 List 工作正常,但 HashSet 返回为空。有点卡住 - 谁能看到我做错了什么?谢谢
最佳答案
更新:
作为 Hans Passant stated有简单的解决方法,只需调用 HashSet.OnDeserialization手动。
var hashset = (HashSet<string>)info.GetValue("hashset", typeof(HashSet<string>));
hashset.OnDeserialization(this);
它还有助于其他通用集合。
据我所知,这可能是 HashSet<T>
中的错误执行。 HashSet
正确序列化为 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
正确恢复。也可以自己查,看:(((System.Collections.Generic.HashSet<string>)(info.m_data[0]))).m_siInfo.m_data[3]
但未能恢复其状态:
它所做的只是存储 SerializationInfo
:
protected HashSet(SerializationInfo info, StreamingContext context)
{
this.m_siInfo = info;
}
可以查看(hashset).m_siInfo.MemberValues[3]
,值由格式化程序正确恢复但未由 HashSet
“解释” .
类似的问题有Dictionary<TKey,TValue>
或者例如LinkedList<T>
.
List<T>
(或类似的基于数组的集合,例如 Stack<T>
)没有问题,因为它们序列化为数组(没有特殊逻辑)。
Workaround was posted by Hans Passant.
恕我直言,BinaryFormatter
不是真正好的和有效的方式来存储值。你可以尝试使用DataContractSerializer (它可以处理此类类型)或使用序列化助手,如 protobuf.net、json.net 等。参见 Why is binary serialization faster than xml serialization?和 Performance Tests of Serializations used by WCF Bindings
关于c# - 序列化 HashSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4192905/