比如public class A
{
   public static void Test()
   {
       //这里怎么获取调用该方法的类信息?,比如下面的B
   }
}public class B
{
   public void New()
   {
      A.Test();
   }
}不用传参数进来应该怎么处理?反射可以做到么?

解决方案 »

  1.   

    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
    foreach (System.Diagnostics.StackFrame sf in st.GetFrames())
    {
            Console.WriteLine(String.Format("File name:{0}, Line:{1}, Column:{2},MethodName:{3}", sf.GetFileName(), sf.GetFileLineNumber(), sf.GetFileColumnNumber(), sf.GetMethod().Name));
    }这么做一下,不需要用反射的。
    (注意事项,如果你的程序发布到其他目录或者别的机器上,相关dll和exe的pdb都要带上,否则filename/linenumber之类的都得不到。
      

  2.   

    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
    for(int i=0;i< st.FrameCount;i++)
    {
    System.Diagnostics.StackFrame sf=st.GetFrame(i);
    Console.WriteLine(String.Format("File name:{0}, Line:{1}, Column:{2},MethodName:{3}", sf.GetFileName(), sf.GetFileLineNumber(), sf.GetFileColumnNumber(), sf.GetMethod().Name));
    }GetFileName();结果为string.Empty;
    Line:0;
    Column:0
    MethodName获取调用该方法所调用的所有方法名字,从最底层开始
      

  3.   

    (注意事项,如果你的程序发布到其他目录或者别的机器上,相关dll和exe的pdb都要带上,否则filename/linenumber之类的都得不到。)而且,你如果在IDE里面,你按F5而不是Ctrl+F5。或者,build之后,把你的.pdb文件,复制到bin\obj\debug目录下,然后ctrl+f5。
      

  4.   

    那你就不要再IDE下面运行,直接运行,看看结果。
    (务必保证你自己所有相关dll的pdb都在同一个目录下面)
      

  5.   

    结果还是
    GetFileName();结果为string.Empty;
    Line:0;
    Column:0
    you are a super man, thanks for you kindness!!!!!!!
      

  6.   

    GetFileName();结果为string.Empty;
    Line:0;
    Column:0这几个不行,方法名获取到了