I'm trying to send a list of abstract objects to% of signalr
Server structure (where all values are generated);
public abstract class Comida : Objeto
{
public int valorNutricional;
public abstract void Comido(Cola jugador);
}
The object class sets the x and y and This class has 2 children that are;
class ComidaNormal : Comida
{
public ComidaNormal(int x, int y)
{
this.x = x;
this.y = y;
this.ancho = 20;
valorNutricional = 1;
}
}
class ComidaEnvenenada : Comida
{
public ComidaEnvenenada(int x, int y)
{
this.x = x;
this.y = y;
this.ancho = 20;
}
}
According to the execution flow they will be instantiated;
public static List<Comida> comidas = new List<Comida>();
comidas.Add(new ComidaEnvenenada(x, y));
comidas.Add(new ComidaNormal(x, y));
Everything works perfectly until you have to send that list of comidas
to the client;
Server method;
public List<Comida> getComida()
{
return comidas;
}
Client method;
comidas = await ApiConexion._hub.Invoke<List<Comida>>("getComida");
skipping the next error in this last line of code.
Error;
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type Snake.Food. Type is an interface or abstract class and can not be instantiated. '