[英]Could not create an instance of type X. Type is an interface or abstract class and cannot be instantiated
Using version 7.0.1 Beta3, I'm trying to serialize/deserialize a complex POCO with properties that are arrays of abstract classes.使用版本 7.0.1 Beta3,我正在尝试序列化/反序列化具有抽象类数组属性的复杂 POCO。 These arrays could contain instance of classes that are derived from abstract ones.
这些数组可以包含派生自抽象类的类的实例。
At serialization, everything seems OK.在序列化时,一切似乎都很好。 The Json fragment below shows that the type information is set correctly.
下面的 Json 片段显示类型信息设置正确。
The Json fragment: Json 片段:
"Items":
[
{
"$type": "IVXB_TS, ...",
"inclusive": true,
"value": "20091231"
}
]
But a deserialization it fails with the following error:但是反序列化失败并出现以下错误:
Could not create an instance of type QTY.无法创建 QTY 类型的实例。 Type is an interface or abstract class and cannot be instantiated.
类型是接口或抽象类,不能实例化。
The class hierarchy is the following :类层次结构如下:
[System.Xml.Serialization.XmlIncludeAttribute(typeof(IVXB_TS))]
public abstract partial class ANY : object, System.ComponentModel.INotifyPropertyChanged
{
}
[System.Xml.Serialization.XmlIncludeAttribute(typeof(IVXB_TS))]
public abstract partial class QTY : ANY
{
}
[System.Xml.Serialization.XmlIncludeAttribute(typeof(IVXB_TS))]
public partial class TS : QTY
{
}
public partial class IVXB_TS : TS
{
}
The Items property :项目属性:
[System.Xml.Serialization.XmlElementAttribute("high", typeof(IVXB_TS))]
[System.Xml.Serialization.XmlElementAttribute("low", typeof(IVXB_TS))]
public QTY[] Items
The type information in the Json fragment seems to not be used. Json 片段中的类型信息似乎没有被使用。 Is this a deserialization configuration issue?
这是反序列化配置问题吗?
The solution to this problem is to configure the deserializer to use the type information in the json .解决这个问题的方法是配置反序列化器使用 json 中的类型信息。 It is not used by default.
默认情况下不使用它。
Serialization is done this way:序列化是这样完成的:
Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Converters.Add(new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter());
serializer.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
serializer.Formatting = Newtonsoft.Json.Formatting.Indented;
using (StreamWriter sw = new StreamWriter(fileName))
using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
{
serializer.Serialize(writer, obj, typeof(MyDocumentType));
}
At deserialization, the settings for TypeNameHandling must be set:在反序列化时,必须设置 TypeNameHandling 的设置:
MyDocumentType obj = Newtonsoft.Json.JsonConvert.DeserializeObject<MyDocumentType>(File.ReadAllText(fileName), new Newtonsoft.Json.JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
});
In my case, setting the TypeNameHandling
to Auto
didn't fix the problem, but setting it to All
fixed the issue.就我而言,将
TypeNameHandling
设置为Auto
并不能解决问题,但将其设置为All
可以解决问题。 As the TypeNameHandling = TypeNameHandling.All
seemed a bit overkill to me, I chose Auto
again, but this time, I passed the type of my root object to the JsonConvert.SerializeObject
function as well:由于
TypeNameHandling = TypeNameHandling.All
对我来说似乎有点矫枉过正,所以我再次选择了Auto
,但这一次,我也将根对象的类型传递给了JsonConvert.SerializeObject
函数:
var settings = new JsonSerializerSettings{ TypeNameHandling = TypeNameHandling.Auto };
var json = JsonConvert.SerializeObject(obj, typeof(ObjType), settings);
var deserializedObj = JsonConvert.DeserializeObject<ObjType>(json, settings);
If you specify Null Value Handling you won't get this error.如果您指定空值处理,您将不会收到此错误。
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}));
You could also set the [JsonIgnore]
flag您还可以设置
[JsonIgnore]
标志
Both of these options worked fine for the use case that brought me here.对于将我带到这里的用例,这两个选项都可以正常工作。
Type name handling wasn't necessary.类型名称处理不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.