本文介绍了反序列化一个List<抽象类>与newtonsoft.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化和反序列化的摘要类(为MustInherit 为VB)的列表,obviusly内它有派生类的唯一实例。

i'm trying to serialize and deserialize a list of abstract classes (mustinherit for vb), obviusly inside it there are only instances of derived classes.

我饰以列表参数 JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)获得看起来像这样的输出:

I've decorated the list parameter with the JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto) obtaining an output that look like this:

但是,当我反序列化不停地说,他无法反序列化抽象类。

But when i deserialize it keep saying that he cannot deserialize an abstract class.

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

删除注释行它的作品!

removing the commented line it works!

推荐答案

这是我的错。我有一个具体的类拿着的名单,但我把这个装饰只在抽象类列表忘记主要原因之一

it was my fault.I had a concrete class holding the List but i put the decorator only in the AbstractClass list forgetting the main one

这篇关于反序列化一个List&LT;抽象类&GT;与newtonsoft.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:35