Похоже, в следующем случае полиморфизм не работает должным образом У меня есть следующие определения:
interface BaseInterface{}
interface NewInterface:BaseInterface{}
class NewClass:NewInterface{}
class GenericClass<T> where T:BaseInterface
{
public string WhoIAm(T anObject)
{
return TestPolymorphism.CheckInterface(anObject);
}
}
class ImplementedClass:GenericClass<NewInterface>{}
class TestPolymorphism
{
public static string CheckInterface(BaseInterface anInterface)
{
return "BaseInterface";
}
public static string CheckInterface(NewInterface anInterface)
{
return "NewInterface";
}
}
Затем, когда я вызываю:
NewClass nc = new NewClass();
ImplementedClass impClass = new ImplementedClass();
Console.WriteLine("The result is " + impClass.WhoIAm(nc));
У меня есть «Результат — BaseInterface»
Я ожидал получить «Результат — NewInterface», поскольку nc реализует BaseClass и NewClass
Как лучше всего получить в результате «NewClass»?
Спасибо