views:

1278

answers:

2

Possible Duplicate:
How can I find the method that called the current method?

How can I get the calling function name from the called function in c#?

+7  A: 
new StackFrame(1, true).GetMethod().Name

Note that in release builds the compiler might inline the method being called, in which case the above code would return the caller of the caller, so to be safe you should decorate your method with:

[MethodImpl(MethodImplOptions.NoInlining)]
Ben M
Beware that walking the stack in this fashion will impose a fairly heavy performance hit. I highly recommend looking for a solution that does not involve a stack walk before making use of one.
jrista
This answer is actually better than the answers in the duplicate question because of the mention of the MethodImpl attribute.
Meta-Knight
A: 

This will get you the name of the method you are in:

string currentMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;

Use with caution since there could be a performance hit.

To get callers:
StackTrace trace = new StackTrace();
int caller = 1;

StackFrame frame = trace.GetFrame(caller);

string callerName = frame.GetMethod().Name;

This uses a stack walk to get the method name. The value of caller is how far up the call stack to go. Be careful not to go to far.

Joe Caffeine
I need which method calls the current method.
Sauron