【发布时间】:2020-12-30 14:40:47
【问题描述】:
我想读入 id,但我不想在读入后设置它。 _processing 变量是在读入文件和反序列化时设置的,因此可以设置它。有没有更优雅的内置方式来处理这个问题?
private string _id;
[JsonProperty(PropertyName = "id")]
public string id
{
get { return _id; }
set
{
if (_processing) // Only allow when reading the file
{
_id = value;
}
}
}
【问题讨论】:
-
private set? -
只需使用私有 setter 和 [JsonProperty]:stackoverflow.com/questions/4066947/private-setters-in-json-net
标签: c# json properties