Reputation: 1841
whenever I try to serialize an object that has an IEnumerable collection I get a big dirty error telling me it can't serialize it because it's an interface. Now I get why it's happening but it raises some other questions for me. Like if I intend on having collections within my objects AND I want to serialize them do I need to resort to
What is the best practice way to go?
Upvotes: 4
Views: 5124
Reputation: 1062865
Speaking as a serializer author, I know exactly why it gets very hard to robustly work just from IEnumerable<T>
, especially for "get-only" properties. You might try IList<T>
(although it wouldn't amaze me if it wants a concrete type such as List<T>
/T[]
), but I suspect the real problem here is that you trying to use one model to do two things, and are unhappy at having to compromise to do it.
Fine: if you don't want to compromise your domain model, write a separate DTO model that is used for serialization, and just map between them. This is usually trivial, and will allow the serializer and the domain model to each excel at their one job. It will also help immensely when you need to "version" the system or introduce a different serializer (JSON, protobuf, etc).
Re your bullets:
Add
etc will workXmlAttributeOverrides
, but watch out for leaking assemblies if you do this)Upvotes: 7
Reputation: 2202
For use interface or derivade classes you MUST use the XmlSerializer(Type type, Type[] extraTypes) constructor.
In extraTypes you MUST include all possible classes which can implement the interfaces in your classes.
Upvotes: 0