views:

366

answers:

6

Well, in a non-static method I could use this.GetType() and it would return the Type. How can I get the same Type in a static method? Of course, I can't just write typeof(ThisTypeName) because ThisTypeName is known only in the runtime. Thanks!

+3  A: 

You can't use this in a static method, so that's not possible directly. However, if you need the type of some object, just call GetType on it and make the this instance a parameter that you have to pass, e.g.:

public class Car {
  public static void Drive(Car c) {
    Console.WriteLine("Driving a {0}", c.GetType());
  }
}

This seems like a poor design, though. Are you sure that you really need to get the type of the instance itself inside of its own static method? That seems a little bizarre. Why not just use an instance method?

public class Car {
  public void Drive() { // Remove parameter; doesn't need to be static.
    Console.WriteLine("Driving a {0}", this.GetType());
  }
}
John Feminella
+11  A: 

If you're looking for a 1 liner that is equivalent to this.GetType() for static methods, try the following.

Type t = MethodBase.GetCurrentMethod().DeclaringType

Although this is likely much more expensive than just using typeof(TheTypeName).

JaredPar
This one works fine. Thanks :) It's not that expensive because it'll be called quite rare.
Entrase
Entrase, by "expensive" Jared means that they are costly for the processor, usually meaning slow. But he said, "much more expensive" meaning slower. Probably not slow at all, unless you are designing a rocket-guidance system.
Yar
I've seen GetCurrentMethod cause some serious performance problems. But since you are just getting the type you can cache it.
Jonathan Allen
A: 

When your member is static, you will always know what type it is part of at runtime. In this case:

class A
{
  public static int GetInt(){}

}
class B : A {}

You cannot call (edit: apparently, you can, see comment below, but you would still be calling into A):

B.GetInt();

because the member is static, it does not play part in inheritance scenarios. Ergo, you always know that the type is A.

Teun D
You *can* call B.GetInt() - at least, you could if it weren't private - but the compile will translate it into a call to A.GetInt(). Try it!
Jon Skeet
Note on Jon's comment: default visibility in C# is private, hence your example doesn't work.
Yar
+2  A: 

I don't understand why you cannot use typeof(ThisTypeName). If this is a non-generic type, then this should work:

class Foo {
   static void Method1 () {
      Type t = typeof (Foo); // Can just hard code this
   }
}

If it's a generic type, then:

class Foo<T> {
    static void Method1 () {
       Type t = typeof (Foo<T>);
    }
}

Am I missing something obvious here?

Tarydon
+9  A: 

There's something that the other answers haven't quite clarified, and which is relevant to your idea of the type only being available at execution time.

If you use a derived type to execute a static member, the real type name is emitted in the binary. So for example, compile this code:

UnicodeEncoding.GetEncoding(0);

Now use ildasm on it... you'll see that the call is emitted like this:

IL_0002: call class [mscorlib]System.Text.Encoding [mscorlib]System.Text.Encoding::GetEncoding(int32)

The compiler has resolved the call to Encoding.GetEncoding - there's no trace of UnicodeEncoding left. That makes your idea of "the current type" nonsensical, I'm afraid.

Jon Skeet
A: 

This is easier... we dont need to expecify the name of class. For instance, I used in toString () method of the parent class... so the each child return his name instead of fixed name of the parent class.

this.GetType().Name
Jaider
There's no “this” in static methods.
Entrase