如何获取当前正在序列化的顶级对象?

时间:2015-03-23 18:55:51

标签: c# json.net

我正在构建一个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实例的内容。它在那里,我想念它,还是有其他方法可以得到它?

0 个答案:

没有答案