Object XmlSerialization with protected property setters

后端 未结 2 1911
我在风中等你
我在风中等你 2021-02-14 09:00

Here is my object


    [Serializable()]
    public class PersistentObject
    {
        public virtual int ID {
            get { return id; }
            protec         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 09:21

    Unfortunately, no. XmlSerializer has some things that are... irritating. This is one of them. Options:

    • use DataContractSerializer (which supports protected etc, but doesn't offer full xml control)
    • annotate with [XmlIgnore] - nothing wrong with it
    • implement IXmlSerializable - hard work and very easy to get wrong
    • take off the setter, and have a separate protected method to set the value
    • use the XmlSerializer constructor that lets you specify everything at runtime; lots of work/maintenance, and you need to manually cache the serializer (otherwise it creates lots of dynamic assemblies)

提交回复
热议问题