Possible Duplicate:
.net XML Serialization - Storing Reference instead of Object Copy
I am creating an attack planner for a game and have the following classes:
public class AttackTypes : ObservableCollection<AttackType> { }
public class AttackType
{
public AttackTypeEnum Type { get; set; }
public Units units { get; set; }
public string Name { get; set; }
public AttackType()
{
units = new Units();
}
}
public class AttackPatterns : ObservableCollection<AttackPattern> { }
public class AttackPattern
{
public string Name { get; set; }
SerializableDictionary<AttackType, int> Count { get; set; }
public AttackPattern(AttackTypes types)
{
Count = new SerializableDictionary<AttackType, int>();
foreach (AttackType item in types) { Count.Add(item, 0); }
}
}
AttackType holds information about the units I want to send. There might be between 5 or 10 distinct types that can be created by the user. An AttackPattern is used to group different AttackTypes. Basically, the pattern has a reference to the full AttackType list and saves how often each AttackType is part of the pattern in a dictionary. When the units for an AttackType are changed, the changes are visible in the patterns as well.
Now I want to save the whole thing to disc and load it when I start the program next time. So far I have been using XML serialization. And there I have to problems. First: The whole structure lives of references. As I said before: When I update the AttackType I need to see the change in the pattern as well. But those ties are lost when I dump it to XML. Second: I serialize the AttackTypes object. All the details about units are in there. When I serialize a pattern, the whole list of AttackTypes (including the units) is saved again. And again for the next pattern. And again for the next pattern...
Any ideas?