Reputation: 961
I have a Config object which I am serializing into XML so I can load it back again later on. In my configuration object I have a dictionary objects
public Dictionary<int, FooConfiguration> Foos
{
get { return foos; }
set
{
if (foos != value)
{
foos = value;
OnPropertyChanged(() => foos);
}
}
}
When I am serializing it doesn't seem to like serializing dictionaries which I can understand, they are complicated objects. So as a workaround I figured I would decorate the property with [XMLIgnore]
and create a list of my objects which would get the values from the dictionary.
public List<FooConfiguration> FooConfigurations
{
get { return Foos.Values.ToList(); }
set
{
int fooNumber = 0;
foreach (var fooConfiguration in value)
{
foos.Add(fooNumber++, fooConfiguration);
}
}
}
This worked as far as serializing goes. I could now see these objects in my XML. The problem is, it was never hitting the setter. Why Would a list be able to serialize but not deserialize?
I have eventually fixed this by using a FooConfiguration[]
instead of a list but this seems a bit weird if you ask me.
As you can see I have a solution, I am just really wanting to understand why its doing what it's doing better.
Upvotes: 0
Views: 77
Reputation: 1064114
If your list has a non-null value, it won't need to hit the setter; it just calls Add
on the list it is given. And in your case, the list you are handing it is detached from the actual data (becuase you created it in the getter).
So basically, it is deserializing the data into an object that is left on the floor.
Upvotes: 2