如果有Abstract类型TransportsystemActionDTO,现在我序列化一个Object并得到以下JSON:
{
"$id": "1",
"$type": "MCC.MFCV2.Services.TransportService2Interfaces.Actions.GenerateGlobalTaskActionDTO, MCC.MFCV2.Services.TransportService2Interfaces",
"Destination": "BA41",
"Source": "BA11",
"Description": "BA11->BA41",
"UseCurrentResource": false,
"ResourceType": null,
"Priority": 0,
"TypeOfTask": 1,
"NextTransportsystemAction": {
"$id": "2",
"$type": "MCC.MFCV2.Services.TransportService2Interfaces.Actions.TriggerGenerateLocalTaskActionDTO, MCC.MFCV2.Services.TransportService2Interfaces",
"Trigger": true,
"ReTrigger": true,
"NextTransportsystemAction": null,
"ConfiguratorPosition": {
"X": 110.0,
"Y": 235.0,
"Width": 187.42666666666668,
"Height": 41.0,
"Z": 0,
"Layer": 0
}
},
"ConfiguratorPosition": {
"X": 105.0,
"Y": 125.0,
"Width": 192.23666666666682,
"Height": 62.000000000000014,
"Z": 0,
"Layer": 0
}
}
现在的问题是,当我使用TransportsystemActionDTO作为基本类型进行反序列化时,我得到以下异常:
无法创建MCC.MFCV2.Services.TransportService2Interfaces.Actions.TransportsystemActionDTO类型的实例。 Type是接口或抽象类,无法实例化。
没关系,但我不知道基类型是什么,可以是不同的类型,但是类型存储在$ type字段中,所以反序列化应该是可能的......
答案 0 :(得分:1)
发现我的错误。我将TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto添加到序列化,但不添加到反序列化。现在它正在运作!