如何在静态方法中获取当前类的名称?
答
new StackFrame().GetMethod().DeclaringType
或
MethodBase.GetCurrentMethod().DeclaringType
或
new StackTrace(true).GetFrame(<frame index>).GetMethod() //e.g. <frame index> = 0
答
我不知道这是否是做的最好的方式,但我通常可以设置一个private
构造函数(如果我的课是static/util不可实例化的类),并且在实例上调用GetType()
。
private MyStaticClass
{
// ...
}
public static Type MyStaticMethiod()
{
return new MyStaticClass().GetType();
}
看:http://stackoverflow.com/questions/552629/c-sharp-print-the-class-name-from-within-a- static-function – k06a 2012-02-17 10:09:15