views:

92

answers:

5

Hi, 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();
+5  A: 

A static method is defined on the type. There is no "this". You'll need to make this an instance method, instead:

class BaseClass {
    public void Ping() {
        Type t = this.GetType(); // This will work, since "this" has meaning here...
    }

You can then do:

class DerivedClass : BaseClass {}

DerivedClass instance = new DerivedClass();
instance.Ping();
Reed Copsey
I need to accomplish this, if it is possible, with a static method.
Marco Bettiolo
@Marco: You'll need to pass in an instance or a type, then. There is no way to get a derived type otherwise.
Reed Copsey
this.GetType() returns BaseClass, and not DerivedClass.
Luca
@Luca: this.GetType() will return the type of this at runtime - which will be the derived class if you're actually in the derived class.
Reed Copsey
A: 

It's not possible to get the derived class from a static method. As an example to illustrate why, imagine BaseClass has 2 subclasses - DerivedClass and AnotherDerivedClass - which one should be returned? Unlike polymorphic non-static methods, there is no possible association with a derived class calling a static method on a base class - the compile time type and runtime type are the same with a static method call.

You can either make the method non-static, so you then get the correct type via polymorphism, or create static method "overrides" in the subclasses, e.g.

class DerivedClass : BaseClass
{
   void Ping() { 
     BaseClass.Ping();
     // or alternatively
     BaseClass.Ping(Type.GetType("DerivedClass"));
   }
}

Your client code can then call the method in the derived class to explicitly indicate it want's the derived class version. If necessary, you might then also pass the DerivedClass type as a parameter to the base class method, to provide context that the method was called via the derived class.

mdma
A: 

Just a guess (not tested)

Type t = MethodBase.GetCurrentMethod().DeclaringType;
SchlaWiener
Just tested, it returns the base class type.
Marco Bettiolo
When you're defining a static method, the method is on the type itself (BaseClass) - there is no instance, hence no way for it to know it's a "derived class"...
Reed Copsey
+2  A: 

If I'm not mistaken, the code emitted for BaseClass.Ping() and DerivedClass.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).

class BaseClass {
    static void Ping<T>() where T : BaseClass {
        Type t = typeof(T);
    }
}

You would call it like this:

BaseClass.Ping<DerivedClass>();
Francis Gagné
This is my actual solution, I was trying to see if I can avoid the method in the derived class.
Marco Bettiolo
A: 

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;
FlyingStreudel