Yas*_*ere 12 c# generics inheritance compiler-errors
我想将从泛型中派生的不同类型的对象添加到基类型列表中.我得到这个编译错误
Error 2 Argument 1: cannot convert from 'ConsoleApplication1.Stable' to 'ConsoleApplication1.ShelterBase<ConsoleApplication1.AnimalBase>' C:\Users\ysn\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs 43 26 ConsoleApplication1
Run Code Online (Sandbox Code Playgroud)
我看不出问题你能为我提供另一种做这种事情的方法吗?
abstract class AnimalBase { public int SomeCommonProperty;}
abstract class ShelterBase<T> where T : AnimalBase
{
public abstract List<T> GetAnimals();
public abstract void FeedAnimals(List<T> animals);
}
class Horse : AnimalBase { }
class Stable : ShelterBase<Horse>
{
public override List<Horse> GetAnimals()
{
return new List<Horse>();
}
public override void FeedAnimals(List<Horse> animals)
{
// feed them
}
}
class Duck : AnimalBase { }
class HenHouse : ShelterBase<Duck>
{
public override List<Duck> GetAnimals()
{
return new List<Duck>();
}
public override void FeedAnimals(List<Duck> animals)
{
// feed them
}
}
class Program
{
static void Main(string[] args)
{
List<ShelterBase<AnimalBase>> shelters = new List<ShelterBase<AnimalBase>>();
///////////////////////////// following two lines do not compile
shelters.Add(new Stable());
shelters.Add(new HenHouse());
/////////////////////////////
foreach (var shelter in shelters)
{
var animals = shelter.GetAnimals();
// do sth with 'animals' collection
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*rth 19
您可以使用逆变,但只有当你改变你的抽象类的接口和返回类型GetAnimals到IEnumerable<T>,因为List<T>不支持此功能.
有效的代码:
abstract class AnimalBase { public int SomeCommonProperty;}
interface IShelterBase<out T> where T : AnimalBase
{
IEnumerable<T> GetAnimals();
}
class Horse : AnimalBase { }
class Stable : IShelterBase<Horse>
{
public IEnumerable<Horse> GetAnimals()
{
return new List<Horse>();
}
}
class Duck : AnimalBase { }
class HenHouse : IShelterBase<Duck>
{
public IEnumerable<Duck> GetAnimals()
{
return new List<Duck>();
}
}
void Main()
{
List<IShelterBase<AnimalBase>> shelters = new List<IShelterBase<AnimalBase>>();
shelters.Add(new Stable());
shelters.Add(new HenHouse());
foreach (var shelter in shelters)
{
var animals = shelter.GetAnimals();
// do something with 'animals' collection
}
}
Run Code Online (Sandbox Code Playgroud)