class MyObj
{
private int _Max;
public object Max
{
get
{
return (int)_Max;
}
set
{
_Max = (int)value;
}
}
}
Program.cs
MyObj obj1 = new MyObj(100);
string json = JsonConvert.SerializeObject(obj1, Formatting.Indented);
obj1.Max = 200;
MyObj obj2 = JsonConvert.DeserializeObject<MyObj>(obj1);
When ran, it crashed on the last line of Program.CS (Deserialize) while doing a Set on the Max property
An exception of type 'System.InvalidCastException' occurred in Supremacy.exe but was not handled in user code
Why does my Set to 200, works but the Deserialize does not? I debugged and the 200 value that is tried to be Set into obj2 is an object containing an int.
If there is no setter on max, Program.cs run properly
Explain me why and how to fix it :)
PS: I'm using box/unboxing because MyObj is part of a hierarchy and it could be any primitive type that would be used as a Max.