Let's say i have two classes:
class Batman
{
public void Robin(){...}
public void Jump(){...}
}
class Superman
{
public void Kryptonie(){...}
public void Jump(){...}
}
Now, I have an instance of those classes:
public object Crossover()
{
var batman = new Batman();
var superman = new Superman();
return superman;
}
I do not know instance of which class the Crossover will return, it could be Batman or Superman.
var someVariableName = Crossover(); //I don't know if this contains an instance of Superman or Batman;
//I do know that no matter which class instance is returned, it will always contain a function named Jump which i want to trigger:
someVariableName.Jump();
Now i know i could do something like:
if (someVariableName.GetType() == typeof(Superman))
{
((Superman) someVariableName).Jump()
}
But is there a way to trigger the Jump function without having to manually check for each type with if..else.., when i know that the instance of the class saved in that variable will always contain a Jump function?