//I know for sure that the animal being passed is a Tiger
protected virtual void Eat<AnimalType>(Animal animal)
where AnimalType : Animal
{
//The animal type is a Tiger type.
//Should be equivalent to :
//Tiger myDerivedAnimal = animal as Tiger;
AnimalType myDerivedAnimal = animal as AnimalType;
if (myDerivedAnimal != null)
{
myDerivedAnimal.eat();
}
}
当我打电话时:
Eat<Tiger>(anAnimalThatIsATiger);
由于某种原因, as cast 正在返回我的空对象。我查看了调试器,传入参数的动物是引用老虎的动物,那么为什么这个演员无法返回我的老虎?截至目前,myDerivedAnimal 填充了默认值(0、null 等)。