【发布时间】:2022-01-07 03:18:58
【问题描述】:
我正在尝试将System.Text.Json 与源代码生成一起使用。不幸的是,我有一个类有一个需要序列化的TimeSpan 数据成员。但是,我无法让它与源生成一起使用,因为它总是试图序列化私有数据成员_ticks。
我不知道是否有办法禁用给定类型的源生成并改用用户提供的源生成。
我什至试图忽略这些字段,但似乎没有考虑到:
[JsonSerializable(typeof(Test))]
partial class Context : JsonSerializerContext
{
}
public class Test
{
public string Name { get; set; }
[JsonIgnore]
public TimeSpan Span { get; set; }
}
仍然报以下错误:
'TimeSpan._ticks' 由于其保护级别而无法访问
您知道是否有解决方法吗?
这是产生错误的源代码生成部分:
这是生成的 TimeSpan 序列化源的一部分,它是导致错误的源,它是生成的 Context.TimeSpan.g.cs 文件的一部分:
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Int64> info11 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Int64>()
{
IsProperty = false,
IsPublic = false,
IsVirtual = false,
DeclaringType = typeof(global::System.TimeSpan),
PropertyTypeInfo = jsonContext.Int64,
Converter = null,
Getter = static (obj) => ((global::System.TimeSpan)obj)._ticks,
Setter = static (obj, value) => global::System.Runtime.CompilerServices.Unsafe.Unbox<global::System.TimeSpan>(obj)._ticks = value!,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = default,
PropertyName = "_ticks",
JsonPropertyName = null
};
【问题讨论】:
-
TimeSpan 在哪里,_ticks 在哪里?
-
TimeSpan 在我的班级中,就像在
Test班级中一样,但是_ticks在System.TimeSpan班级中
标签: c# serialization system.text.json sourcegenerators