1

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?

Community
  • 1
  • 1
Jan
  • 2,168
  • 2
  • 19
  • 28
  • 1
    http://stackoverflow.com/questions/1617528/net-xml-serialization-storing-reference-instead-of-object-copy – LightStriker Nov 15 '12 at 08:05
  • I've closed as duplicate, because this is the same underlying question, and Darin's answer addresses both questions equally. If there is something I missed that changes between the two, add a comment here including @marc (so I get notified) – Marc Gravell Nov 15 '12 at 08:08

0 Answers0