views:

1395

answers:

2

I have a classes like:

[Serializable]
public class child {
     public Parent parent;
}

[Serializable]
public class Parent {
  public List<child> children;
}

When I deserialize Parent, I want each of each children to have a reference to it's parent. Question is, where in the deserialization process can I set the child's "parent" pointer? I can't seem to use a custom constructor for child, because deserialization always uses the default constructor. If I implement ISerializable, then it seems that the child objects have already been created by the time the parent is created. Is there another way to achieve this?

+1  A: 

If you leave it alone and let Parent be a public read/write property of the Child class, .NET automatic serialization process will handle it properly.

Mehrdad Afshari
For binary serialization, properties are not considered. Only fields. But yes, it will still be handled automatically.
leppie
A: 

I accomplished this (sort of) by overriding the Add method in the child object's collection class, to 'set" a property value in the child class with the unique identifer of the parent object

 public class Connections: List<Connection>
    {       public new void Add(Connection connection)
        {
            connection.ApplicationName = ApplicationName;
            base.Add(connection);
        }
    }
Charles Bretana