views:

88

answers:

2

I'm having a problem where one Dictionary in my project is either not serializing or not deserializing. After deserializing, the data I serialized is simply not in the object.

Here's the relevant snip of the class being serialized:

class Person : ISerializable {
    private Dictionary<Relation,List<int>> Relationships = new Dictionary<Relation,List<int>>();

    public Person(SerializationInfo info, StreamingContext context) {
        this.Relationships = (Dictionary<Relation, List<int>>) info.GetValue("Relationships", typeof(Dictionary<Relation, List<int>>));
        }

    public void GetObjectData(SerializationInfo info, StreamingContext context) {
        info.AddValue("Relationships", this.Relationships);
        }
    }

Note, this is binary serialization. Everything else in the project serializes and deserialzes correctly.

+1  A: 

This works fine for me:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

class Program
{
    [Serializable]
    class Relation
    {
    }

    [Serializable]
    class Person
    {
        private Dictionary<Relation, List<int>> Relationships = new Dictionary<Relation, List<int>>();

        public Person() {}

        public void AddRelationship() {
            Relationships.Add(new Relation(), new List<int>());
        }

        public int CountRelations()
        {
            return Relationships.Count;
        }

        /*
        public Person(SerializationInfo info, StreamingContext context)
        {
            this.Relationships = (Dictionary<Relation, List<int>>)info.GetValue("Relationships", typeof(Dictionary<Relation, List<int>>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Relationships", this.Relationships);
        }
         * */
    }

    public static void Main()
    {
        Person person = new Person();
        person.AddRelationship();
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();
        formatter.Serialize(stream, person);
        stream.Seek(0, SeekOrigin.Begin);
        person = (Person)formatter.Deserialize(stream);
        Console.WriteLine("Count: " + person.CountRelations());
    }
}

Note: the second constructor and the GetObjectData are not required, although adding them doesn't break anything either. If I am oversimplifying your code, perhaps you can post a modified version of some working, runnable code that demonstrates the failure you are observing. You can either remove things from your code until it's simple enough to post a compiling version without requiring external classes, or add things to mine until it reproduces the error - whichever is easier for you.

You may also find that in the process of making a simple runnable version that reproduces the error, that you are able to solve the problem yourself.

Mark Byers
A: 

maybe, there is a problem with your relation class, does it have circular references to other objects??

LostMohican
It's an enum...
Shadow
so there is not : )
LostMohican