IXmlSerializable and inheritance

前端 未结 2 520
死守一世寂寞
死守一世寂寞 2021-01-13 21:21

Let\'s say that I have two classes, a base class and a derived class. They are fairly simple classes, and mainly just act as data structures (with the derived class obviousl

相关标签:
2条回答
  • 2021-01-13 21:24

    Well, I've managed to do what you're trying to do using the DataContractSerializer.

    You should either put KnownTypes attribute on the base classes you want to serialize, or better yet, use a DataContractResolver to find the derived types.

    I've seen a generic implementation on the web, I'll post it here if I find it again.

    Edit

    Found it here

    Let me know if you need more help.

    0 讨论(0)
  • 2021-01-13 21:37

    XmlSerialization is not polymorphic (the member attributes, however, go with the declaring class; i.e. unless you overrule a member with the new keyword (in contrast to override) you'll 'inherit' the serialization attributes.

    Deserialization always results in the actual type that was serialized. This makes a lot of sense, really.

    0 讨论(0)
提交回复
热议问题