I have a strange problem when serialising custom data objects via DataContracts and restoring them. I have the following class in which the problem occurs:
[DataContract]
public class ProjectManager
{
// list of projects
[DataMember]
public List<Project> ProjectList { get; set; }
// active project
[DataMember]
public Project ActiveProject { get; set; }
// instance variable
[DataMember]
private static ProjectManager instance = null;
ActiveProject saves a reference to one object from ProjectList. The problem is that in one case the reference saved in ActiveProject seems not to be equal to the reference in the ProjectList, though there is only one in the list. If I change my ActiveProject, my object in ProjectList is not changed.
This problem occurs ONLY after deserialising data and I don't know what I can do to prevent this error from happening. Is this behaviour possible or must there be any crossreferences in my code that I didn't find yet?
Usually deserialization does not take care of 2 objects referencing the same objects. Instead it may create a new object at each reference location.
Try binary serialization / deserialization.
If it still fails, you can create your own serialization, and create an object reference list. As soon as you serialize an object with a reference, that you already stored before (serialized before) then you could add a special tag, and so you would be able to rebuild the reference to the same object during deserialization.
But that is the next thing I plan to add to my serialization, so.. time for custom serialization may be too long. Hopefully binary serialization is sufficient
Another workaround would be to work with a list and to have an integer (refering to the correct index number) instead of a second reference. Or build any other variable that helps you to rebuild double-references