So I currently have the following code
if (File.Exists(file))
{
IList<Cart> temp = JsonConvert
.DeserializeObject<IList<Cart>>(File.ReadAllText(file));
foreach (var x in temp)
{
crt.Add(x);
}
}
With class Cart
being the base class and Drinks
and Food
being its derivation classes with its own different variables. When I use the above code to store the results of deserialization into crt, which is an list of cart with both Drinks and Food objects, the results when I try to display each item inside the cart becomes Cart_Project.Model.Cart
rather than showing the variables of its derived classes. I've tried to use
foreach(var x in temp)
{
if (x is Food)
{
crt.Add(x as Food);
}
else if (x is Drinks)
{
crt.Add(x as Drinks);
}
}
To add each object as its own derived class, but nothing shows up when I try to display it on my app. Is theres any way to read each object from the file and store it into a List of its own class?