【发布时间】:2014-06-18 22:00:24
【问题描述】:
我最近解决了这个错误:
Type is an interface or abstract class and cannot be instantiated,
通过使用此处建议的解决方案:
Using Json.NET converters to deserialize properties.
不幸的是,我遇到了新的问题。在下面的代码中,Depth 作为反序列化的一部分被调用,反序列化又调用 Prof 属性,该属性失败,因为 'this.Profile' 为空。 ('this.Profile' 是继承自 Section 类的属性。)
public class TimberSection : Section, IPurlinShape, IEavesBeamShape
{
private Profiles.Flat Prof
{
get
{
var prof = this.Profile as Profiles.Flat;
if (prof != null)
return prof;
else
throw new DataMissingException("Expected Rectangle profile");
}
}
public override double Depth { get { return Prof.Depth; } }
}
我使用http://www.jsoneditoronline.org/ 查看我的对象,似乎一切都正确。当我反序列化时出现 null 的配置文件对象的值与序列化之前的对象相同。
下面是我序列化它的方法,然后立即反序列化它。
string serialisedEnquiry = JsonConvert.SerializeObject(enquiry, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
Enquiry enq = JsonConvert.DeserializeObject<Enquiry>(serialisedEnquiry, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
我在这里做错了什么?
【问题讨论】: