我在 C# 中遇到多态性问题。我有一个实现接口的对象,但是我不能将对象的集合表示为接口的集合。这违背了我对多态性的理解。所以我想知道我哪里出错了。
[TestFixture]
class Tester
{
[Test]
public void Polymorphism()
{
var list = new List<Foo> {new Foo {Name = "Item"}};
Assert.That(list, Is.InstanceOf<IList>());
Assert.That(list[0], Is.InstanceOf<Foo>());
Assert.That(list[0], Is.InstanceOf<IBar>());
// why are the rest true but this false?
Assert.That(list, Is.InstanceOf<IList<IBar>>());
}
}
internal interface IBar
{
}
internal class Foo : IBar
{
public string Name { get; set; }
}