开发者

XML serialization and inheritance

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜