23

Normally I can call this.GetType(), but I can't access this in a static method. How can we check it?

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • 4
    Look: http://stackoverflow.com/questions/552629/c-sharp-print-the-class-name-from-within-a-static-function – k06a Feb 17 '12 at 10:09

2 Answers2

38
new StackFrame().GetMethod().DeclaringType

or

MethodBase.GetCurrentMethod().DeclaringType

or

new StackTrace(true).GetFrame(<frame index>).GetMethod() //e.g. <frame index> = 0
alexsuslin
  • 4,130
  • 1
  • 20
  • 30
13

Use typeof:

string className = typeof(MyClass).Name;
brgerner
  • 4,287
  • 4
  • 23
  • 38