【发布时间】:2016-05-17 12:50:08
【问题描述】:
在封闭类中实现 ISerializable 时,我在获得 Dictionary 的反序列化/序列化工作时遇到问题。如果我只应用 SerializableAttribute,它似乎能够自动反序列化。但是,我需要在此过程中检查反序列化字典,因此我需要 ISerializable 才能工作。
我设置了一个小测试,以确保它不是由于其他一些问题。 Test 类如下所示:
[Serializable]
class Test : ISerializable
{
private Dictionary<string, int> _dict;
public Test()
{
var r = new Random();
_dict = new Dictionary<string, int>()
{
{ "one", r.Next(10) },
{ "two", r.Next(10) },
{ "thr", r.Next(10) },
{ "fou", r.Next(10) },
{ "fiv", r.Next(10) }
};
}
protected Test(SerializationInfo info, StreamingContext context)
{
// Here _dict.Count == 0
// So it found a Dictionary but no content?
_dict = (Dictionary<string, int>)info.GetValue("foo", typeof(Dictionary<string, int>));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("foo", _dict, typeof(Dictionary<string, int>));
}
public override string ToString()
{
var sb = new StringBuilder();
foreach (var pair in _dict)
sb.Append(pair.Key).Append(" : ").Append(pair.Value).AppendLine();
return sb.ToString();
}
}
还有 main 来测试它:
static void Main(string[] args)
{
var t1 = new Test();
Console.WriteLine(t1);
var formatter = new BinaryFormatter();
using (var stream = new FileStream("test.test", FileMode.Create, FileAccess.Write, FileShare.None))
formatter.Serialize(stream, t1);
Test t2;
using (var stream = new FileStream("test.test", FileMode.Open, FileAccess.Read, FileShare.Read))
t2 = (Test)formatter.Deserialize(stream);
Console.WriteLine(t2);
Console.ReadLine();
}
控制台中的输出前后一致。但正如 Test 类中所述,重载的构造函数不会读取反序列化字典中的任何内容。
我做错了什么还是这是一个错误/微妙的副作用?
【问题讨论】:
-
我的问题不在于是否可以序列化,而是如何“控制”序列化过程(使用 ISerializable 的实现),这似乎失败了。跨度>
-
@OrelEraki:OP 询问二进制序列化,特别是反序列化实例的有效性。提供的链接不涉及此特定区域。
-
@OrelEraki:我确实浏览了答案。第二个链接中的答案没有解决字典二进制序列化作为复合对象的成员,这是这个问题的本质。
标签: c# .net dictionary serialization