我还没有看到关于Json.NET支持与反序列化对象只读
字段的信息。我也注意到,.NET DataContract和DataMember属性允许deserializtion中填充只读
字段,但Json.NET似乎并不支持这一点,从我的行为至少敢看。
I haven't seen much information about Json.NET supporting deserializing objects with readonly
fields. I do notice that the .NET DataContract and DataMember attributes allow for populating readonly
fields during deserializtion, but Json.NET doesn't seem to support this, at least from the behavior I'm seeing.
英辉可以看到变化的一个字段设置为只读
导致空
反序列化后的价值。我有另一个问题工作示例(修改如下图所示),这就是我看到的行为。
afai can see changing a field to readonly
results in a null
value after deserialization. I had a working sample for another question (modified as shown below), and that's the behaviour I see.
public class NameAndId
{
public string name;
public int id;
}
public class Data
{
public NameAndId[] data;
}
public class Target
{
public string id;
public readonly NameAndId from;
public DateTime updated_time;
public readonly string message;
public Data likes;
}
public class Program
{
static void Main(string[] args)
{
string json = File.ReadAllText(@"c:\temp\json.txt");
Target newTarget = JsonConvert.DeserializeObject<Target>(json);
}
}