C# – Find out what class invoked a method

.netc++class

Is there any way, in C#, for a class or method to know who (i.e. what class/ method) invoked it?

For example, I might have

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

class b{
   public string run(){
        **CODE HERE**
   }
}

Output:
"Invoked by the 'test' method of class 'a'."

Best Solution

StackFrame

var frame = new StackFrame(1);

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