好的,我有一些从基类派生的不同对象,我已经将它们中的一堆放在了一个列表中。我想遍历列表并将每个推送到一个方法。我对每个人的类型签名都有单独的方法,但是编译器在抱怨。有人可以解释为什么吗?这是使用泛型的机会吗?如果是,如何使用?
class Base { }
class Level1 : Base { }
class Level2 : Level1 { }
...
List<Base> oList = new List<Base>();
oList.Add(new Level1());
oList.Add(new Level2());
...
...
foreach(Base o in oList)
{
DoMethod(o);
}
...
void DoMethod(Level1 item) { }
void DoMethod(Level2 item) { }
我究竟做错了什么?