I haven't been able to find the answer to this anywhere, but when I try to serialize a struct or class with static or const member variables, they don't serialize by default. If I try to force serialization by setting MemberSerialization.OptIn
, I get an error.
ex.
[JsonObject(MemberSerialization.OptIn)]
public class Test
{
[JsonProperty]
public int x = 1;
[JsonProperty]
public static int y = 2;
}
If I try to serialize this class with:
Test t = new Test();
string s = JsonConvert.SerializeObject( t );
I get the error Error getting value from 'y' on 'Test'
. The same happens if y is const.
My theory is that static and const values are stored somewhere special in memory, and for some reason the Json serializer dies trying to access them. That's entirely a hunch though, and I see nothing in the C# Reference for Static that's of any help. I'm relatively new to C# - and this is really a curiosity question more than anything at this point.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…