@SamualDavis在一个相关的问题中提供了一个很好的解决方案,我将在这里进行总结。
如果必须将JSON流反序列化为具有接口属性的具体类,则可以 将具体类作为参数包含在该类的构造函数中! NewtonSoft反序列化器足够聪明,可以弄清楚它需要使用那些具体的类来反序列化属性。
这是一个例子:
public class Visit : IVisit
{
/// <summary>
/// This constructor is required for the JSON deserializer to be able
/// to identify concrete classes to use when deserializing the interface properties.
/// </summary>
public Visit(MyLocation location, Guest guest)
{
Location = location;
Guest = guest;
}
public long VisitId { get; set; }
public ILocation Location { get; set; }
public DateTime VisitDate { get; set; }
public IGuest Guest { get; set; }
}