views:

1285

answers:

3

I encountered a, at least to my expectations, strange behavior in the binary serialization of .NET.

All items of a Dictionary that are loaded are added to their parent AFTER the OnDeserialization callback. In contrast List does the other way. This can be really annoying in real world repository code, for example when you need to add some delegates to dictionary items. Please check the example code and watch the asserts.

Is it normal behaviour?

[Serializable]
public class Data : IDeserializationCallback
{
    public List<string> List { get; set; }

    public Dictionary<string, string> Dictionary { get; set; }

    public Data()
    {
        Dictionary = new Dictionary<string, string> { { "hello", "hello" }, { "CU", "CU" } };
        List = new List<string> { "hello", "CU" };
    }

    public static Data Load(string filename)
    {
        using (Stream stream = File.OpenRead(filename))
        {
            Data result = (Data)new BinaryFormatter().Deserialize(stream);
            TestsLengthsOfDataStructures(result);

            return result;
        }
    }

    public void Save(string fileName)
    {
        using (Stream stream = File.Create(fileName))
        {
            new BinaryFormatter().Serialize(stream, this);
        }
    }

    public void OnDeserialization(object sender)
    {
        TestsLengthsOfDataStructures(this);
    }

    private static void TestsLengthsOfDataStructures(Data data)
    {
        Debug.Assert(data.List.Count == 2, "List");
        Debug.Assert(data.Dictionary.Count == 2, "Dictionary");
    }
}
+2  A: 

Interesting... for info, I tried it with the attribute-based approach (below), and it behaves the same... very curious! I can't explain it - I'm just replying to confirm reproduced, and to mention the [OnDeserialized] behaviour:

[OnDeserialized] // note still not added yet...
private void OnDeserialized(StreamingContext context) {...}

Edit - found "connect" issue here. Try adding to your callback:

Dictionary.OnDeserialization(this);
Marc Gravell
+2  A: 

I can reproduce the problem. Had a look around Google and found this: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94265 although I'm not sure it's the exact same problem, it seems pretty similar.

EDIT:

I think that adding this code may have fixed the problem?

    public void OnDeserialization(object sender)
    {
            this.Dictionary.OnDeserialization(sender);
    }

No time to exhaustively test, and I want to beat Marc to the answer ;-)

endian
I think we found it at at about the same time - maybe you pipped me, so +1 to you ;-p
Marc Gravell
Actually, my post pointed that out well before your edit :(
Kent Boogaart
And +1 to both of you :-)
endian
+4  A: 

Yes, you've discovered an annoying quirk in Dictionary<TKey, TValue> deserialization. You can get around it by manually calling the dictionary's OnDeserialization() method:

public void OnDeserialization(object sender)
{
    Dictionary.OnDeserialization(this);
    TestsLengthsOfDataStructures(this);
}

Incidentally, you can also use the [OnDeserialized] attribute rather than IDeserializationCallback:

[OnDeserialized]
public void OnDeserialization(StreamingContext context)
{
    Dictionary.OnDeserialization(this);
    TestsLengthsOfDataStructures(this);
}

HTH, Kent

Kent Boogaart
No slight intended, Kent; +1
Marc Gravell