51

Possible Duplicate:
.NET: Determine the type of “this” class in its static method

How can I make GetType() accessible from a static method?

I have this abstract base class

abstract class MyBase
{
   public static void MyMethod()
   {
      var myActualType = GetType(); // this is an instance method
      doSomethingWith(myActualType);
   }
}

and an implementation of that class. (I could have many implementations.)

class MyImplementation : MyBase 
{
    // stuff
}

How can I get myActualType to be typeof(MyImplementation)?

Community
  • 1
  • 1
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 3
    I don't think you can do this in a simple matter. A question though, why would you want this? The point with static methods is that they don't need a instance to run, right? So why would it's type matter? Maybe your problem can be solved in a different way altogether. – Alxandr Oct 20 '11 at 17:23
  • 1
    I'm trying to write a convenience method to register routes for an mvc application - I'm trying to restrict them by namespace. Its abstract because I want your application to own the concrete implementation. – Daniel A. White Oct 20 '11 at 17:24
  • Then just don't write it as static, but as a method instead. Then you can easily get the type by means of `GetType()`. – Alxandr Oct 28 '11 at 16:32
  • 1
    @Alxandr - i dont want to instantiate a new controller. – Daniel A. White Oct 28 '11 at 16:42
  • Hmm. I see your point. You are making attribute-based routing I presume? (btw, that already exists, though I don't remember where, SO uses it amongst others). However, have you considered trying to solve the problem a bit differently? What I did was to go trough all decedents of IController in the entire assembly and use reflection on the classes to find the routes, it's pretty simple. – Alxandr Oct 28 '11 at 16:49

3 Answers3

59

The "type" within a static method is always the specific type, since there is no such thing as a virtual static method.

In your case, this means you can just write:

 var myActualType = typeof(MyBase);

Since the "type" of MyMethod, being a static, is always a static method of MyBase.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Hi Reed - I think that the issue with your solution above is that `myActualType` would be set to the `MyBase` type, regardless of the subclass type. If I understand the original question correctly, I think the poster is trying to get the `MyMethod` method to use the type of the current class. – matt Oct 21 '11 at 11:51
  • 3
    @mjd79: Yes - but that's my point - a static method is always going to be typed in the base type, since a static method is part of the type, not the class using the method... – Reed Copsey Oct 21 '11 at 16:20
  • For those like me who want an equivalent of Java `MyType.class`, this is the answer for you. – Guillaume Perrot Sep 10 '16 at 02:46
25

What about this?

abstract class MyBase<T>
{
   public static void MyMethod()
   {
      var myActualType = typeof(T);
      doSomethingWith(myActualType);
   }
}


class MyImplementation : MyBase<MyImplementation>
{
    // stuff
}
matt
  • 9,113
  • 3
  • 44
  • 46
  • 5
    What stops you from doing `class Bar : MyBase`? – Reed Copsey Oct 20 '11 at 17:30
  • Ah, that's an excellent point! I suppose convention is the only thing that makes this work, but you're right, if someone declares the class that way, the `MyMethod` method won't work as expected. – matt Oct 21 '11 at 11:52
  • 3
    We could do `where T : ` according to https://msdn.microsoft.com/en-us/library/d5x73970.aspx – John May 23 '15 at 16:41
3

This is the pattern i used.

abstract class MyBase
{
   public static void MyMethod(Type type)
   {
      doSomethingWith(type);
   }
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445