我想知道,因为可以使用反射完成很多事情,我可以在构造函数完成执行后更改私有只读字段吗?
(注意:只是好奇心)
public class Foo
{
private readonly int bar;
public Foo(int num)
{
bar = num;
}
public int GetBar()
{
return bar;
}
}
Foo foo = new Foo(123);
Console.WriteLine(foo.GetBar()); // display 123
// reflection code here...
Console.WriteLine(foo.GetBar()); // display 456
Run Code Online (Sandbox Code Playgroud) 我没有看到很多关于Json.NET支持使用readonly字段反序列化对象的信息.我注意到.NET DataContract和DataMember属性允许readonly在反序列化期间填充字段,但Json.NET似乎不支持这一点,至少从我看到的行为来看.