This question already has answers here:
Closed 4 years ago.
Normally I can call this.GetType(), but I can't access this in a static method. How can we check it?
new StackFrame().GetMethod().DeclaringType
or
MethodBase.GetCurrentMethod().DeclaringType
or
new StackTrace(true).GetFrame(<frame index>).GetMethod() //e.g. <frame index> = 0
Use typeof:
string className = typeof(MyClass).Name;
I don't know if it's the best way to do it, but I usually set a private
constructor (if my class is a static/util non-instantiable class) and than call GetType()
on an instance.
private MyStaticClass
{
// ...
}
public static Type MyStaticMethiod()
{
return new MyStaticClass().GetType();
}