2

我有一个看起来像这样的包装列表:

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

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

    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();
    }
}

我在这样的客户类中包含一个字段实例:

[Serializable]
public class Customer
{
    public Customer()
    { }

    private OrderManager _orders
        = new OrderManager();
    public OrderManager Orders
    {
        get { return _orders; }
        set { _orders = value; }
    }
}

我像这样创建 JSON.NET 序列化程序:

private JsonSerializer GetSerializer()
{
    var serializer = new JsonSerializer();

    // on or off the type name handling doesn't help
    //serializer.TypeNameHandling = TypeNameHandling.All;

    var contractResolver = new DefaultContractResolver(true);
    contractResolver.IgnoreSerializableAttribute = false;
    contractResolver.IgnoreSerializableInterface = false;

    serializer.ContractResolver = contractResolver;

    serializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
    serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    return serializer;          
}

客户对象的序列化有效,但在反序列化期间出现错误:

在“Tests.SerializationTests+Customer”上将值设置为“_orders”时出错。

将 TypeNameHandling 设置为 All 我得到这个:

Type specified in JSON 'Tests.SerializationTests+Order[], Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'Tests.SerializationTests+OrderManager, Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Path '_orders.$type', line 1, position 232.

知道如何让 Json.NET 与包装列表一起玩吗?

4

3 回答 3

1

我想你需要实施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);
    }
}
于 2012-05-07T20:21:28.420 回答
0

尝试以下两件事之一:

在您的列表周围创建一个名为 OrderCollection 的包装器集合类:

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

或者尝试将 LIST 变成 ARRAY。

让我们知道其中一个是如何工作的。

于 2012-05-07T20:15:33.530 回答
0

我不确定序列化是否适用于 IEnumerable,你能用 List 试试吗?

[Serializable]
public class OrderManager : List<Order>
于 2012-05-07T20:16:23.383 回答