I defined the basic tree structure, in order to facilitate extension, USES generics:
Nodes:
Public class Node {
Public string ID {get; set; }
Public string Description {get; set; }
Public IList
Children;//-- -- -- -- -- - the problem here } Tree object: Public class Tree T: : Node, where the Node { . The method of tree object } When actual use, such as menu definition as follows: Protected Tree Menus. Normal when serialization: {" Parent ", null, "Children" : [{" Parent ", null, "Children" : [{" Parent ", null, "Children", null, "ID" : "Menu", "Description" : "Menu", "Index" : 0, "IsValid" : false}, {" Parent ", null, "Children", null, "ID" : "Setup", "Description" : "system configuration", "Index" : 0, "IsValid" : false}], "ID" : "Web", "Description" : "Web console", "Index" : 0, "IsValid" : false}], "ID", null, "Description", null, "Index" : 0, "IsValid" : false} But during deserialization, due to the Children of the node is defined as IList System. Text. Json deserialized instance using the Node object, not the MenuItem object, Could you tell me how to define the Node class, can be successfully used in subsequent extension MenuItem? CodePudding user response:
Well,,,, it is not a type, After the yourself into the Node, converted into a hole in a tree Tree CodePudding user response:
During deserialization, child nodes resulting object is a Node, unable to convert MenuItem The current writing: Public class MenuItem: Node { } Can think of a solution: Public class MenuItem: Node { Public new IList Children;//-- -- -- -- -- - cover the Children of the parent, but it obviously violates the inherited ideas, why not just write separately for each different class } CodePudding user response:
refer to the second floor shangwg response: during deserialization, child nodes resulting object is a Node, unable to convert MenuItem The current writing: Public class MenuItem: Node { } Can think of a solution: Public class MenuItem: Node { Public new IList Children;//-- -- -- -- -- - cover the Children of the parent, but it obviously violates the inherited ideas, why not just write separately for each different class } What do you direct this Var node=JsonConvert. DeserializeObject (json); CodePudding user response:
Does Var node=JsonConvert. DeserializeObject (json); Can be serialized to the specified object type, but to a tree structure (MenuItem type is from the Node type extension), the Node's Children, at the same time, the child Node as defined as IList Instead, will be serialized as Node MenuItem, unless overridden definition as: IList , so that all expand the Node object to cover the Children property, lost the significance of inheriting, Now I try to use the container to do, haven't finished validation (if there are other problems, such as RPC transmission instantiation corresponding database structure problem)