i would like to get the type of the derived class from a static method of its base class.
How can this be accomplished?
Thanks!
class BaseClass {
static void Ping () {
Type t = this.GetType(); // should be DerivedClass, but it is not possible with a static method
}
}
class DerivedClass : BaseClass {}
// somewhere in the code
DerivedClass.Ping();
If I’m not mistaken, the code emitted for
BaseClass.Ping()andDerivedClass.Ping()is the same, so making the method static without giving it any arguments won’t work. Try passing the type as an argument or through a generic type parameter (on which you can enforce an inheritance constraint).You would call it like this: