【发布时间】:2026-02-21 06:40:01
【问题描述】:
我有这个类层次结构:
public class ProxyBotsSnapshotLogEntryDetails : IBotsSnapshotLogEntryDetails
{
public ICollection<IBotSnapshot> Snapshots { get; set; }
}
public class ProxyBotSnapshot : IBotSnapshot
{
public string Name { get; set; }
public ICollection<IBotSnapshotItem> States { get; set; }
}
public class ProxyBotSnapshotItem : IBotSnapshotItem
{
public int Count { get; set; }
public IrcBotChannelStateEnum State { get; set; }
}
及其对应的接口
public interface IBotsSnapshotLogEntryDetails
{
ICollection<IBotSnapshot> Snapshots { get; set; }
}
public interface IBotSnapshot
{
string Name { get; set; }
ICollection<IBotSnapshotItem> States { get; set; }
}
public interface IBotSnapshotItem
{
int Count { get; set; }
IrcBotChannelStateEnum State { get; set; }
}
我想从 JSON 反序列化:
var test = JsonConvert.DeserializeObject<ProxyBotsSnapshotLogEntryDetails>(entry.DetailsSerialized);
但我收到一条错误消息,提示 Newtonsoft 无法转换接口。
我发现了这篇很有前途的文章:
https://www.c-sharpcorner.com/UploadFile/20c06b/deserializing-interface-properties-with-json-net/
但我不确定如何使用该属性,因为在我的情况下,该属性是一个接口列表。
【问题讨论】:
-
你能贴出你是如何创建 ProxybotsSnapshotLogEntryDetails 对象的代码
标签: c# .net json serialization json.net