我有以下课程:
public class Person
{
}
public class Employee : Person
{
}
public class Customer : Person
{
}
使用这些类的一些方法:
public class OtherClass
{
public void DoSomething(Employee e)
{
}
public void DoSomething(Customer c)
{
}
}
来电:
// People = Collection<Person>.
foreach (var p in People)
DoSomething(p); // Should call the right method at runtime and "see" if it´s an Employee or a Customer.
编译器不允许这样做。我如何实现这个场景?