泛型树结构子节点列表的反序列化问题
我定义了基础的树结构,为了便于扩展,使用了泛型:
节点:
public class Node{
public string ID{get;set;}
public string Description{get;set;}
public IList<Node> Children;//------问题出在这里
}
树对象:
public class Tree<T>:Node,where T:Node{
......树对象的各种方法
}
实际使用时,比如菜单如下定义:
protected Tree<MenuItem> menus;
序列化时正常:
{"Parent":null,"Children":[{"Parent":null,"Children":[{"Parent":null,"Children":null,"ID":"Menu","Description":"菜单设置","Index":0,"IsValid":false},{"Parent":null,"Children":null,"ID":"Setup","Description":"系统配置","Index":0,"IsValid":false}],"ID":"Web","Description":"Web控制台","Index":0,"IsValid":false}],"ID":null,"Description":null,"Index":0,"IsValid":false}
但是在反序列化时,由于节点的Children定义为IList<Node>
System.Text.Json反序列化实例时使用了Node对象,并非MenuItem对象。
请问该怎么定义Node类,才能在后续扩展中顺利使用MenuItem呢?