Can I do something like this? (Over-simplified because it’s too complex)
abstract class A {
public string Print() {}
public static string DoPrint(A a, Type T) {
((T)a).Print(); // <-- This should call eg. A3.Print(), not A.Print()
}
}
class A1: A {
public new string Print() {}
}
class A2: A {
}
class A3: A {
public new string Print() {}
}
class Somewhere
{
A3 a3 = new A3();
a3.DoPrint();
}
I have many classes (A1, A2, A3, and so on) that inherits from a base class (A)
I am trying to create the DoPrint() function above in class A.
Can it be done?
I tried this
public static string DoPrint(A a) {
a.Print();
}
but it calls A.Print(), not A3.Print()
EDIT: Changed the title from “Pass a type as parameter?” because everyone was right, I could just use virtual (thank you!). The problem lied somewhere else, irrelevant to this question.
What’s wrong with normal virtual methods and inheritance?? Look at the use of virtual and override: