我正在尝试序列化和反序列化一个abstract类列表(mustinherit对于vb),其中只有派生类的实例.
我已经使用JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)获得如下所示的输出来装饰list参数:
但是当我反序列化时,它一直说他不能反序列化抽象类.
http://james.newtonking.com/json/help/index.html?topic=html/SerializeTypeNameHandling.htm
public class ConcreteClass
{
private ObservableCollection<AbstractClass> _Nodes = new ObservableCollection<AbstractClass>();
//<Newtonsoft.Json.JsonProperty(itemtypenamehandling:=Newtonsoft.Json.TypeNameHandling.Auto)>
public ObservableCollection<AbstractClass> Nodes {
get { return this._Nodes; }
}
public string Name { get; set; }
public int Id { get; set; }
}
public abstract class AbstractClass
{
private ObservableCollection<AbstractClass> _Nodes = new ObservableCollection<AbstractClass>();
[Newtonsoft.Json.JsonProperty(itemtypenamehandling = Newtonsoft.Json.TypeNameHandling.Auto)]
public ObservableCollection<AbstractClass> Nodes {
get { return this._Nodes; }
}
}
Run Code Online (Sandbox Code Playgroud)
删除它起作用的注释行!
我想创建一个可排序的observableCollection,所以我开始创建一个继承observable的类,用一些方法对它进行排序,然后我希望该类将索引保存到子节点中,所以我创建了一个接口,公开了一个索引属性,其中我可以写入,并且我将我的集合类的T表示为我的接口,然后我希望能够从avery项目访问parentCollection,这里问题已经开始,因为父集合的类型是通用的...我已经尝试了很多解决方案,我认为协方差或不变性是方法,但我不能让它工作......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class SortableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>, ISortableCollection<T> where T : ISortable<T>
{
public void Sort()
{
//We all know how to sort something
throw new NotImplementedException();
}
protected override void InsertItem(int index, T item)
{
item.Index = index;
item.ParentCollection = this;
base.InsertItem(index, item);
}
}
public interface ISortableCollection<T> : IList<T>
{
void Sort();
}
public interface ISortable<T>
{
Int32 Index { get; set; }
ISortableCollection<T> ParentCollection { …Run Code Online (Sandbox Code Playgroud)