tags:

views:

292

answers:

6
+3  Q: 

C# static "this"

Is there a way in a C# static method to refer to the Type the method is defined in?

In an instance method you can determine the type by:

public void Foo()
{
    Type type = this.GetType();
}

how would it look like in a static method?

public static void Bar()
{
    Type type = ....?
}

Update: Sorry, clarification needed: I know the typeof(...) feature. I'm looking for a keyword or code that gives me the Type without explicitly referencing the class name.

Update: Besides Developer Art's answer, which does exactly what I was looking for, is there a simpler way?

+10  A: 
public class Foo {
   public static void Bar() {
      Type type = typeof(Foo);
   }
}

You could also use the stacktrace

public class Foo {
   public static void Bar() {
        Type type = new StackTrace().GetFrame(0).GetMethod().DeclaringType;
   }
}
m0sa
var Abuse!!! :)
SysAdmin
no var abuse anymore :)
m0sa
@m0sa - I have seen instances where the optimizer omits entries from the stack trace, and if that happens the second method here will not work. See here for more info: http://blogs.msdn.com/b/jmstall/archive/2005/03/20/399287.aspx
Steve Townsend
Everything was clearly identified on the right... var isn't abused if it's clear in it's intentions. 4 lines instead of 1 might be considered abuse... but var abuse? hardly
WernerCD
@Steve thanks for the link, I was not aware of this.
m0sa
@m0sa - I found out the hard way when someone in my team was using this to identify a type this way. Worked in Debug but not Release.
Steve Townsend
+3  A: 
class ClassA
{
    public static void Bar()
    {
        Type t = typeof(ClassA);
    }
}

Captain Obvious, I know.

Grozz
+1  A: 

Why don't you use typeof?

public class Foo
{
  public static void Bar()
  {
    Type type = typeof(Foo);
  }
}

Or like Developer Art suggested it, you could do it using reflection, but it will be slower.

using System.Reflection;
public class Foo
{
  public static void Bar()
  {
    Type type = MethodBase.GetCurrentMethod().DeclaringType();
  }
}
madgnome
+1  A: 

You could use

 Type t = typeof ( Program );

A very unusual way would be to use a StackTrace, taking the Type of the last frame...

Sascha
+11  A: 

Here you go:

public static void Bar ()
{
  Type type = System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType ();
}
Developer Art
Although this method is generic it is more costly than a simple, static 'typeof (ClassName)'.
Paul Ruane
The author is obviously interested in getting the type without specifying its name explicitly.
Developer Art
Does this require a `[MethodImpl(MethodImplOptions.NoInlining)]` decoration to prevent getting the wrong method?
Ani
I don't think so. This might only be needed if you used the GetFrame method.
Developer Art
@Developer Art: Exactly, that's what I'm interested in. This solution looks appropriate and safe, saver than the StackTrace approach. Does that mean that there is no simpler way? No keyword, e.g.?
chiccodoro
I'm not aware of one.
Developer Art
+1  A: 

It's a bit convoluted, but I think you could also go the route of a singleton-type pattern

public class Foo
{
    private static Foo _Instance = new Foo();
    public static Type Bar()
    {
        return _Instance.GetType();
    }
}
Steven
Hi Steven, if I created a private static field which refers explicitly to the enclosing type, then I'd rather use `private static Type _thisType = typeof(Foo);`
chiccodoro
That works too :) I was just throwing some alternative methods out there. If the scenerio ever came up again where you needed a static version of "this", then this approach gives you that....If the type is all you'll ever need, than any of these approaches gives you that pretty well.
Steven
Hi Steven, sorry, I didn't read your code well enough and didn't realize that your Bar() method exactly does that, just providing the Type. I though that was "my" static method that *needs* the type.
chiccodoro
Still my "proposal" would be even simpler. A one-liner good to copy and paste. If you want you can incorporate it in your answer as a 2nd variant.
chiccodoro
@chiccodoro - I don't think I explained myself very clearly, so I apologize if I was vague. I don't disagree that what you're suggesting is simpler, and if easy access to the Type is all you'll ever need, then the static '_Instance' is probably overkill. I was just throwing this out there based on the title of the question. *if* you ever needed the static equivalent of a "this" for some other reason, then having something like a static _Instance provides you with that...
Steven