I have object like below
public class MyObject
{
private IEnumerable _data;
public MyObject(IEnumerable<int> data)
{
_data = data;
}
public IEnumerable GetData()
{
return this._data;
}
}
the _data
property is private. I am storing this object in session as below using jsonserializer
var val = new MyObject(new int[] {1,2,3})
HttpContext.Session.SetString("MyKey", JsonConvert.SerializeObject(val));
then im trying to retrieve it as below
var val = HttpContext.Session.GetString("MyKey");
var myObject = JsonConvert.DeserializeObject<MyObject>(val);
However MyObject.GetData()
returns null. I am assuming since _data
property is private JsonSerializer is not able to serialize it.
So what are my options here to store an object in session which has private properties?