Serializing object that inherits from List

前端 未结 1 1082
面向向阳花
面向向阳花 2021-01-19 08:24

When I try to serialize this collection, the name property is not serialized.

public class BCollection : List where T : B_Button
{       
          


        
相关标签:
1条回答
  • 2021-01-19 09:03

    In many serializers (and data-binding, in fact), an object is either an entity or (exclusive) a list; having properties on a list is not commonly supported. I would refactor to encapsulate the list:

    public class Foo<T> {
        public string Name {get;set;}
        private readonly List<T> items = new List<T>();
        public List<T> Items { get { return items; } }
    }
    

    Also; how would you plan on representing that in JSON? IIRC the JSON array syntax doesn't allow for extra properties either.

    0 讨论(0)
提交回复
热议问题