Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current class' name in a static method? [duplicate]

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

like image 952
Louis Rhys Avatar asked Feb 17 '12 10:02

Louis Rhys


2 Answers

new StackFrame().GetMethod().DeclaringType

or

MethodBase.GetCurrentMethod().DeclaringType

or

new StackTrace(true).GetFrame(<frame index>).GetMethod() //e.g. <frame index> = 0
like image 160
alexsuslin Avatar answered Oct 26 '22 14:10

alexsuslin


Use typeof:

string className = typeof(MyClass).Name;
like image 13
brgerner Avatar answered Oct 26 '22 15:10

brgerner