开发者

C# serialization dropping property in subclass?

When I try to serialize a populated instance of ty开发者_StackOverflowpe List<C>() where:

public class A<T> : List<T>
{
     [XmlAttribute("Name")]
     public string Name {get; set;}
     public A() {}
}

public class B
{
    [XmlAttribute("Other")]
    public string OtherPCO { get; set:}
}

public class C : A<B>
{

}

The serialization drops the Name property of class A but does create an array of type B with the OtherPCO property. How can I get the serializer to include Name?


Collections are serialized in a specific manner, which takes into account only the items of the collection, not the extra properties you added to the class. You need to wrap the collection in another class that is not a collection.

This should give you the desired result :

public class A<T>
{
     [XmlAttribute("Name")]
     public string Name {get; set;}

     [XmlElement(typeof(T))]
     public List<T> Items { get; set; }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜