有人能解释一下它是怎么工作的吗?我甚至使用反射来设置B值,但它说B没有设置值。Newtonsoft.Json如何实现这一点?
public class A
{
private int a = 10;
public int B => a;
}
var a = new A();
var json = JsonConvert.SerializeObject(a);//json={"B":10}
//my expectation: this will have error, because B only have getter, dont have setter
//in reality: this run OK, and aa.B = aa.a = 10
var aa = JsonConvert.DeserializeObject<A>(json);
字符串
1条答案
按热度按时间6ioyuze21#
它实际上并没有反序列化B属性。它之所以“工作”,是因为默认情况下是
a == 10
。以下面的代码为例:字符串