Speaking as the author of a serializer, I know for sure why it is very difficult to reliably work only with IEnumerable<T> , especially for receive-only properties. You can try IList<T> (although this would not surprise me if he wanted to get a specific type, for example List<T> / T[] ), but I suspect that the real problem is that you are trying to use one a model for performing two actions, and are unhappy that they have to compromise to do this.
Great: if you donβt want to compromise your domain model, write a separate DTO model that is used for serialization, and simply map between them. This is usually trivial and will allow the serializer and domain models to succeed on the same job. It will also help a lot when you need a "version" of the system or enter another serializer (JSON, protobuf, etc.).
Return your bullets:
- I suspect any particular type of list (even your own) using
Add , etc. will work - I do not recommend this to anyone - it hurts to do it reliably
- nothing to guess about attributes; again, I suspect that your complaint concerns the assignment of your domain model - like this: fine, do not do this - have a separate model; you can do all this at runtime, but it works a lot more (see
XmlAttributeOverrides , but watch out for assembly XmlAttributeOverrides if you do) - Do not underestimate how much work; the basics are seductively easy; but non-trivial scenarios can be cruel.
source share