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.