现在的位置: 首页 > 综合 > 正文

得到调用方法的类。

2013年03月12日 ⁄ 综合 ⁄ 共 291字 ⁄ 字号 评论关闭
class A
{
  B b;
  public void GetSomething()
  {
     b=new B();
     b.GetSomething();
  }
}
calss B
{
  public void GetSomething()
  {
    StackTrace st = new StackTrace(false);
    StackFrame sf = st.GetFrame(1);
    return sf.GetMethod().ReflectedType.Name; //将会输出 A,也就得到了类A的名字。
  //其实从sf.GetMethod()可以得到绝大多数调用GetSomething()的信息
  }
}

抱歉!评论已关闭.