c# inheriting generic collection and serialization

前端 未结 5 2150
礼貌的吻别
礼貌的吻别 2020-12-06 13:36

The setup:

class Item
{
    private int _value;

    public Item()
    {
        _value = 0;
    }

    public int Value { get { return _value; } set { _valu         


        
5条回答
  •  猫巷女王i
    2020-12-06 14:09

    This behavior is "By Design". When deriving from a collection class the Xml Seralizier will only serialize the collection elements. To work around this you should create a class that encapsulates the collection and the name and have that serialized.

    class Wrapper
    {
        private Collection _items;
        private string _name;
    
        public Collection Items { get {return _items; } set { _items = value; } }
        public string Name { get { return _name; } set { _name = value; } }
    }
    

    A detailed discussion is available here: http://blogs.vertigo.com/personal/chris/Blog/archive/2008/02/01/xml-serializing-a-derived-collection.aspx

提交回复
热议问题