我正在构建一个CustomCreationConverter
子类,它反序列化依赖于其包含对象的对象:
public interface IFoo {
public int Id { get; }
public IEnumerable<IBar> Bars { get; }
}
public interface IBar {
public int Id { get; }
public IFoo Parent { get; }
}
public class BarDeserializer : CustomCreationConverter<IBar>
{
private class Bar : IBar
{
public Int Id { get; set; }
public IFoo Parent { get; set; }
}
public override IBar Create(Type objectType)
{
return new Bar();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var b = (Bar) Create(objectType);
b.Id = JToken.Load(reader).Value<int>();
b.Parent = ??? // can I get the IFoo from anything in this context?
return b;
}
}
我在JsonSerializer
中没有看到任何可以提供当前正在反序列化的IFoo
实例的内容。它在那里,我想念它,还是有其他方法可以得到它?