我正在尝试将 json 反序列化为一个对象模型,其中集合表示为IList<T>
类型。
实际的反序列化在这里:
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<IList<Contact>>(
(new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd()));
在我发布异常之前,我让你应该知道隐式转换是什么。这是Contact
类型:
public class Contact
{
public int ID { get; set; }
public string Name { get; set; }
public LazyList<ContactDetail> Details { get; set; }
//public List<ContactDetail> Details { get; set; }
}
这是ContactDetail
类型:
public class ContactDetail
{
public int ID { get; set; }
public int OrderIndex { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
重要的是要知道LazyList<T>
它实现IList<T>
:
public class LazyList<T> : IList<T>
{
private IQueryable<T> _query = null;
private IList<T> _inner = null;
private int? _iqueryableCountCache = null;
public LazyList()
{
this._inner = new List<T>();
}
public LazyList(IList<T> inner)
{
this._inner = inner;
}
public LazyList(IQueryable<T> query)
{
if (query == null)
throw new ArgumentNullException();
this._query = query;
}
现在这个LazyList<T>
类定义很好,直到我尝试将 Json 反序列化到它中。System.Web.Script.Serialization.JavaScriptSerializer
似乎想要将列表序列化为有意义的列表,因为List<T>
它的年龄但我需要它们的类型IList<T>
,以便它们将转换为我的LazyList<T>
(至少那是我认为我出错的地方)。
我得到这个例外:
System.ArgumentException: Object of type 'System.Collections.Generic.List`1[ContactDetail]' cannot be converted to type 'LazyList`1[ContactDetail]'..
当我尝试List<ContactDetail>
在我的Contact
类型中使用时(如您在上面所见),它似乎有效。但我不想使用List<T>
's. 我什至尝试让我的LazyList<T>
继承List<T>
似乎执行,但是将List<T>
' 内部传递T[]
给我的实现是一场噩梦,我根本不希望List<T>
我的模型中任何地方都膨胀。
我还尝试了其他一些 json库但无济于事(我可能没有充分利用这些库。我或多或少地替换了引用并尝试重复此问题顶部引用的代码。也许传递设置参数将帮助??)。
我不知道现在该尝试什么。我要使用另一个解串器吗?我是否调整反序列化本身?我需要改变我的类型来取悦反序列化器吗?我是否需要更多地担心隐式转换或只是实现另一个接口?