JSON.NET cannot deserialize a wrapped collection

扶醉桌前 提交于 2019-12-06 13:03:57

I guess you need to implement ICollection<Order>

public class OrderManager : IEnumerable<Order>,ICollection<Order>

-

[Serializable]
public class OrderManager : IEnumerable<Order>,ICollection<Order>
{
    public OrderManager()
    { }

    private List<Order> orders = new List<Order>();

    public Order this[int i]
    {
        set { AddOrder(value); }
        get { return orders[i]; }
    }

    public void AddOrder(Order order)
    {
        orders.Add(order);
    }

    public IEnumerator<Order> GetEnumerator()
    {
        return orders.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return orders.GetEnumerator();
    }

    public void Add(Order item)
    {
        AddOrder(item);
    }

    public void Clear()
    {
        orders.Clear();
    }

    public bool Contains(Order item)
    {
        return orders.Contains(item);
    }

    public void CopyTo(Order[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public int Count
    {
        get { return orders.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(Order item)
    {
        return orders.Remove(item);
    }
}

Try one of these two things:

Make a wrapper collection class around your List called OrderCollection:

[Serializable]/[DataContract]
public OrderCollection : List<Order> { ... }

Or try turning the LIST into an ARRAY.

Let us know how either one works.

I am not sure serialization works with IEnumerable, could you try it with a List?

[Serializable]
public class OrderManager : List<Order>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!