找出哪个类调用了一个方法

Chr*_*ell 4 .net c# class

有没有办法,在C#中,一个类或方法知道谁(即什么类/方法)调用它?

例如,我可能有

class a{
   public void test(){
         b temp = new b();
         string output = temp.run();
   }
}

class b{
   public string run(){
        **CODE HERE**
   }
}
Run Code Online (Sandbox Code Playgroud)

输出:"由'a'类的'test'方法调用."

Jim*_*mmy 13

的StackFrame

var frame = new StackFrame(1);

Console.WriteLine("Called by method '{0}' of class '{1}'",
    frame.GetMethod(),
    frame.GetMethod().DeclaringType.Name);
Run Code Online (Sandbox Code Playgroud)