3

How can I get the type (not a name string, but a type itself) of the current class, in a static method of an abstract class?

using System.Reflection; // I'll need it, right?

public abstract class AbstractClass {

    private static void Method() {

        // I want to get CurrentClass type here

    }

}

public class CurrentClass : AbstractClass {

    public void DoStuff() {

        Method(); // Here I'm calling it

    }

}

This question is very similar to this one:

How to get the current class name at runtime?

However, I want to get this information from inside the static method.

Community
  • 1
  • 1
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • Take a look at this: [GetType in static method](http://stackoverflow.com/questions/7839691/gettype-in-static-method) – Zbigniew Apr 20 '13 at 13:15

4 Answers4

3
public abstract class AbstractClass
{
    protected static void Method<T>() where T : AbstractClass
    {
        Type t = typeof (T);

    }
}

public class CurrentClass : AbstractClass
{

    public void DoStuff()
    {
        Method<CurrentClass>(); // Here I'm calling it
    }

}

You can gain access to the derived type from the static method simply by passing the type as a generic type argument to the base class.

User 12345678
  • 7,714
  • 2
  • 28
  • 46
  • Yeah, I've figured to use generics myself in the meantime. – Max Yankov Apr 20 '13 at 13:23
  • 2
    If you use generics like that, consider what will happen if you've got `ExtendedClass : CurrentClass`: `Method` will get `CurrentClass`, not `ExtendedClass`. – Tim S. Apr 20 '13 at 13:31
1

I think you will have to either pass it in like the other suggestion or create a stack frame, I believe if you put an entire stack trace together though it can be expensive.

See http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

Ian
  • 33,605
  • 26
  • 118
  • 198
1

if you are calling this static method only from derived classes you can use 'System.Diagnostics.StackTrace' like

abstract class A
{
    public abstract string F();
    protected static string S()
    {
        var st = new StackTrace();
        // this is what you are asking for
        var callingType = st.GetFrame(1).GetMethod().DeclaringType;
        return callingType.Name;
    }
}

class B : A
{
    public override string F()
    {
        return S(); // returns "B"
    }
}

class C : A
{
    public override string F()
    {
        return S();  // returns "C"
    }
}
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
0

The method can't be static if you're going to call it without passing in a type. You can do this:

public abstract class AbstractClass {
    protected void Method() {
        var t = GetType(); // it's CurrentClass
    }
}

If you also need it to be accessible from a static context, you can add an overload, even a generic overload, e.g.:

public abstract class AbstractClass {
    protected static void Method<T>() {
        Method(typeof(T));
    }
    protected static void Method(Type t) {
        // put your logic here
    }
    protected void Method() {
        Method(GetType());
    }
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122