【发布时间】:2021-01-20 08:54:11
【问题描述】:
我有一个用于字典的自定义 JsonConverter,其中包含抽象键和值:
public class DictionaryJsonConverter<TKey, TValue> : JsonConverter<Dictionary<TKey, TValue>>
我是这样使用的:
[JsonConverter(typeof(DictionaryJsonConverter<ServerDevice, long>))]
public static Dictionary<ServerDevice, long> KnownServers { get; set; }
但是 read 和 write 方法都没有被调用。我错过了什么吗?
错误重现示例:
https://dotnetfiddle.net/2VakI3(不在 DotNetFiddle 中编译)
我不保证读取或写入方法是正确的,因为我从未见过结果。
【问题讨论】:
-
请创建一个可重现的示例。见minimal reproducible example。
-
@DavidL 添加示例
-
当编译器为 .Net 5 - dotnetfiddle.net/g1yCAF 时,此代码将在 DNF 中编译
-
好的,将转换器添加到
DefaultSerializationOptions执行写入(在此示例中)并在反序列化时读取。但为什么有必要呢?