Friday, February 23, 2007

Get Calling Function.

i was trying out something today and i needed to get the name of function which calls my function.
Here was the senario , i had a function A which calls B which inturn call C. i wanted the name of function B in C.

Here is how i did it.

public void C()
{
StackFrame obj = new StackFrame(1);
MessageBox.Show(obj.GetMethod().Name);

}

You can give the number of StackFrames to skip in the StackFrame constructor to go back furthur.