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 BaseCla
In short this is what I tried to answer here. It uses one interface to unify all derived classes and let them all call the same static method from the base class without passing a Type.
public interface IDerived
{
}
public class BaseClass<T> where T : IDerived
{
public static void Ping()
{
//here you have T = the derived type
}
}
public class DerivedClass : BaseClass<DerivedClass>, IDerived
{
//with : BaseClass<DerivedClass> we are defining the T above
}
public class ExampleApp
{
public void Main()
{
//here we can call the BaseClass's static method through DerivedClass
//and it will know what Type calls it
DerivedClass.Ping();
}
}
Why not just use the methods that are there already?
If you have
class BaseClass {}
partial class DerivedClass : BaseClass {}
You can look at
DerivedClass.GetType().BaseType;