Hi all,
I've recently experienced what seems to be odd inconsistencies between the capabilities of the IntermediateSerializer and the automatically generated XNB type readers. The issue pertains specifically to collection types. IntermediateSerializer handles collection-type properties adequately, for instance in the following:
| |
| public class MyDataType |
| { |
| private readonly Collection<string> data = new Collection<string>(); |
| |
| public Collection<string> Data |
| { |
| get { return this.data; } |
| } |
| } |
Even though the property is read-only, since it is a collection type, both XmlSerializer and IntermediateSerializer follow the most sensible behavior of serializing collection items via the Add() method without needing to replace the backing store via a setter method.
Apparently, however, automatically generated XNB serializers don't follow through on the promise. The above data type successfully compiles through the content pipeline, and serializes/deserializes correctly using IntermediateSerializer. However, upon runtime loading (via ContentManager) of a non-empty XML collection of the data-type above, the created runtime instance will have 0 items, indicating that the property data was somehow ignored.
More specifically, the only way I got it to work with collection data-types was using the following declaration:
| |
| public class MyDataType |
| { |
| public List<string> Data = new List<string>(); |
| } |
This declaration successfully works with both IntermediateSerializer AND automatic XNB serialization. However it doesn't seem to work with the Collection generic base class. More freaky still is that if you use the first declaration pattern with a List<> instead of a Collection<> the ContentPipeline blows up with a circular dependency of some sort. Weird stuff.
Has anyone experienced anything of the sort? I find these inconsistencies too hard to believe, but I managed to break it down to a really concrete and simple "now works/now doesn't work" example and I can't see where I could have gone wrong in my analysis.
Thanks for all the help in advance and best regards,
Gonçalo