我正在使用 Json.NET
对一个包含私有字段类型为 Guid 和公共属性的对象进行反序列化。当我的 json 中 Guid
对应的值为空时,我想将 Guid.Empty
分配给我的字段。
public class MyClass
{
private Guid property;
public Guid Property
{
get { return property; }
set
{
if (value == null)
{
property = Guid.Empty;
}
else
{
property = value;
}
}
}
}
但是反序列化
需要访问私有字段,因为当我尝试反序列化时会出现以下错误:
将值为 {null} 转换为类型 'System.Guid' 时出错。路径 '[0].property',行 6,位置 26。
我该如何使它忽略私有字段并改用公共属性呢?