How to obtain a function name?

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

I have two functions A & B. The function A calls function
B. How can I programatically determine within body of
function B the name of function (in our case it is A) that
function B was called by?

public void A()
{
B();
}

public void B()
{
string name;
name = ??? (Must be A)

}

Thanks,
Alex
 
Alex,
I have two functions A & B. The function A calls function
B. How can I programatically determine within body of
function B the name of function (in our case it is A) that
function B was called by?

You can get that with the StackTrace class. Something like

new StackTrace().GetFrame( 1 ).GetMethod().Name

But note that capturing a stack trace can be expensive, so don't
overuse this technique.



Mattias
 
Back
Top