如何获得当前类的名字在静态方法? [英] How to get the current class' name in a static method?

查看:162
本文介绍了如何获得当前类的名字在静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,我可以调用this.GetType(),但我不能访问该在静态方法。我们该如​​何检查呢?

解决方案

 新的StackFrame()。GetMethod的()。DeclaringType
 

  MethodBase.GetCurrentMethod()。DeclaringType
 

 新的堆栈跟踪(真).GetFrame(小于帧索引>)。GetMethod的()//如: <帧索引> = 0
 

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

这篇关于如何获得当前类的名字在静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆