开发者

XML serialization and inheritance

开发者 https://www.devze.com 2023-01-31 01:18 出处:网络
class A : IXmlSerializable {... } class B : A { public string X; } When i serialize object of type B, only A is serialized. How can I i开发者_运维技巧nclude X with minimum effort?The fact you\'re i
class A : IXmlSerializable
{     ...
}

class B : A
{
   public string X;
}

When i serialize object of type B, only A is serialized. How can I i开发者_运维技巧nclude X with minimum effort?


The fact you're implementing IXmlSerializable makes it a bit harder...

What you could do is make WriteXml and ReadXml virtual, and override them in your derived class:

public class A : IXmlSerializable
{
    public virtual void WriteXml (XmlWriter writer)
    {
        // Serialize A members
    }

    public virtual void ReadXml (XmlReader reader)
    {
        // Deserialize A members
    }

    public XmlSchema GetSchema()
    {
        return(null);
    }


}


public class B : A
{
    public override void WriteXml (XmlWriter writer)
    {
        base.WriteXml(writer);

        // Serialize B members
    }

    public virtual void ReadXml (XmlReader reader)
    {
        base.ReadXml(reader);

        // Deserialize B members
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号