Retrieving the calling method name from a method

Possible duplicate:
How to find the method that called the current method?

I have a method in an object that is called from several places inside the object. There is a quick and easy way to get the name of a method called this popular method.

Pseudocode EXAMPLE:

public Main() { PopularMethod(); } public ButtonClick(object sender, EventArgs e) { PopularMethod(); } public Button2Click(object sender, EventArgs e) { PopularMethod(); } public void PopularMethod() { //Get calling method name } 

Inside PopularMethod() I would like to see the value Main if it was called from Main ... I would like to see " ButtonClick " if PopularMethod() was called from ButtonClick

I looked at System.Reflection.MethodBase.GetCurrentMethod() , but that will not call me the calling method. I looked at the StackTrace class, but I really didnโ€™t like to run the entire stack trace every time this method is called.

+43
reflection methods c # calling-convention
Mar 05 '09 at 18:22
source share
7 answers

I do not think that this can be done without a stack trace. However, it is quite simple:

 StackTrace stackTrace = new StackTrace(); MethodBase methodBase = stackTrace.GetFrame(1).GetMethod(); Console.WriteLine(methodBase.Name); // eg 

However, I think you really need to stop and ask yourself if necessary.

+66
Mar 05 '09 at 18:29
source share

In .NET 4.5 / C # 5, this is simple:

 public void PopularMethod([CallerMemberName] string caller = null) { // look at caller } 

Compiler automatically adds the name of the caller; So:

 void Foo() { PopularMethod(); } 

will be held in "Foo" .

+99
Jan 02 '13 at 13:08
source share

It is really very simple.

 public void PopularMethod() { var currentMethod = System.Reflection.MethodInfo .GetCurrentMethod(); // as MethodBase } 

But be careful, I'm a little skeptical that the inlay of the method has some effect. You can do this to make sure that the JIT compiler will not interfere.

 [System.Runtime.CompilerServices.MethodImpl( System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] public void PopularMethod() { var currentMethod = System.Reflection.MethodInfo .GetCurrentMethod(); } 

To get the calling method:

 [System.Runtime.CompilerServices.MethodImpl( System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] public void PopularMethod() { // 1 == skip frames, false = no file info var callingMethod = new System.Diagnostics.StackTrace(1, false) .GetFrame(0).GetMethod(); } 
+16
Mar 05 '09 at 18:27
source share

Just enter the parameter

 public void PopularMethod(object sender) { } 

IMO: If this is good enough for events, it should be enough for that.

+5
Mar 05 '09 at 18:30
source share

I often found that I want this myself, but I always finish reorganizing the design of my system, so I donโ€™t get the anti-wagging tail dog pattern. The result has always been more robust architecture.

+4
Jul 25 '12 at 12:16
source share

While you can most definitely trace the stack and understand it that way, I would urge you to rethink your design. If your method needs to know about some kind of โ€œstateโ€, I would say just create an enumeration or something else and take this as a parameter for your PopularMethod (). Something like that. Based on what you post, stack tracking will be excessive IMO.

+1
Mar 05 '09 at 18:34
source share

I think you need to use the StackTrace class and then StackFrame.GetMethod() in the next frame.

It seems weird to use Reflection . If you define PopularMethod , you cannot define a parameter or something to convey the information you need. (Or put in a base class or something else ...)

0
Mar 05 '09 at 18:34
source share



All Articles