使用 ISerializable 反序列化 .NET 字典

本文关键字:字典 NET 反序列化 ISerializable 使用 | 更新日期: 2023-09-27 18:33:33

在封闭类中实现 ISerializable 时,我在获取字典的反序列化工作时遇到问题。如果我只应用可序列化属性,它似乎能够自动反序列化。但是,我需要在此过程中检查反序列化的字典,因此我需要 ISerialize 才能正常工作。

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

[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();
    }
}

主要测试一下:

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 反序列化 .NET 字典

Dictionary<TKey, TValue>实现IDeserializationCallback并推迟其反序列化的完成,直到整个对象图被读回。你可以看到它是如何在参考源上实际实现的:

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>,也许很少有其他容器类型表现出相同的行为