使用ISerializable

时间:2016-02-07 22:13:34

标签: c# .net dictionary serialization

在封闭类中实现ISerializable时,我遇到了使字典解除/序列化工作的问题。如果我只应用SerializableAttribute,它似乎能够自动解序/序列化。我需要在过程中检查反序列化的字典,所以我需要ISerializable来工作。

我设置了一个小测试,以确保它不是由于其他一些问题。 测试类如下所示:

[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 类中所评论的那样,重载的构造函数不会读取反序列化词典中的任何内容。

我做错了什么或这是一个错误/微妙的副作用?

1 个答案:

答案 0 :(得分:2)

Dictionary<TKey, TValue>实现IDeserializationCallback并推迟完成其反序列化,直到读回整个对象图。您可以在Reference Source上看到它的实际实现方式:

protected Dictionary(SerializationInfo info, StreamingContext context)            

{
    //We can't do anything with the keys and values until the entire graph has been deserialized
    //and we have a resonable estimate that GetHashCode is not going to fail.  For the time being,
    //we'll just cache this.  The graph is not valid until OnDeserialization has been called.
    HashHelpers.SerializationInfoTable.Add(this, info);
}

要在代码中强制完成调用_dict.OnDeserialization()

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>));

    _dict.OnDeserialization(null);

    // Content is restored.
    Console.WriteLine("_dict.Count={0}", _dict.Count);
}

PS:HashSet<T>SortedSet<T>LinkedList<T>可能很少有其他容器类型表现出相同的行为