Given the code below:
class Animal
{ }
class Dog : Animal
{ }
class Cage<T>
{
private T animal;
public Cage(T animal)
{
this.animal = animal;
}
public T Animal
{
get { return animal;}
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Cage<Animal> animalCage = new Cage<Animal>(dog);
Cage<Dog> dogCage = (Cage<Dog>)animalCage;
}
}
How can I workaround the last compiler error (conversion from animalCage to dogCage)?
In my code I know that the cage contains a dog, but I'm not able to find a way to cast to it. Is my unique alternative to create a converter and create a new Cage<Dog> instance from the value of a Cage<Animal>?