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'."
StackFrame
var frame = new StackFrame(1);
Console.WriteLine("Called by method '{0}' of class '{1}'",
frame.GetMethod(),
frame.GetMethod().DeclaringType.Name);
You can look at the stack trace to determine who called it.
http://msdn.microsoft.com/en-us/library/system.environment.stacktrace.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With