Blog Stats
  • Posts - 62
  • Articles - 1
  • Comments - 32
  • Trackbacks - 64

 

Finding calling method using reflection

There's probably a more elegant way to do this, but I wanted to find out which method had called the current method for logging. For example, I am calling a method in my DAO and i want to know which method from my Service layer called it...the Stack trace caught during an exception cuts me off and I could only find snippets of getting the current method name for logging purposes.

So, here's a method I drummed up to get the name of the calling-calling method:

/// <summary>

/// Helper method for getting the name of the method that got us here

/// </summary>

/// <param name="currentMethod"></param>

/// <returns></returns>

private string GetPreviousMethodName(MethodBase currentMethod)

{

string methodName = string.Empty;

try

{

StackTrace sTrace = new System.Diagnostics.StackTrace(true);

//loop through all the stack frames

for (Int32 frameCount = 0; frameCount < sTrace.FrameCount; frameCount++)

{

StackFrame sFrame = sTrace.GetFrame(frameCount);

System.Reflection.MethodBase thisMethod = sFrame.GetMethod();

//If the Type in the frame is the type that is being searched

if (thisMethod == currentMethod)

{

if (frameCount + 1 <= sTrace.FrameCount)

{

StackFrame prevFrame = sTrace.GetFrame(frameCount + 1);

System.Reflection.MethodBase prevMethod = prevFrame.GetMethod();

methodName = prevMethod.ReflectedType + "." + prevMethod.Name;

//get the method and its parameter info

//then exit out of the for loop

}

break;

}

}

}

catch (Exception)

{

//swallow all exceptions this may encounter...this is informational and mroe for convenience anyways

return string.Empty;

}

return methodName;

 

}


Feedback

# re: Finding calling method using reflection

Gravatar Separation of concerns. 6/30/2006 4:56 AM | Don Pannick

# RE: Finding calling method using reflection

Gravatar Exactly what I was looking for...I don't know a lot about reflection, so I wonder if there is a more 8/11/2006 3:20 PM | Community Blogs

# re: Finding calling method using reflection

Gravatar How about:

return new StackFrame(2, false).GetMethod()

8/11/2006 1:00 PM | Haacked

# re: Finding calling method using reflection

Gravatar Whoops! That should probably be

return new StackFrame(1, false).GetMethod()

In my code, I had a method called GetCallingMethod() which uses this technique, so it has to pass in 2 to be useful. 8/11/2006 1:51 PM | Haacked

# re: Finding calling method using reflection

Gravatar Glad it could serve some value. I wrote this on the fly early on and it served my purpose. Elegance wasn't on my mind...this is more COors than McEwans. :) 8/13/2006 6:37 PM | Mike

# re: Finding calling method using reflection

Gravatar I found a simpler example on the internet, at:
http://www.codeproject.com/dotnet/MethodName.asp


using System.Diagnostics;
using System.Reflection;

[STAThread]
static void Main(string[] args)
{
WhatsMyName();
}
// function to display its name
private static void WhatsMyName()
{
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
Console.WriteLine(methodBase.Name ); // Displays “WhatsmyName”
WhoCalledMe();
}
// Function to display parent function
private static void WhoCalledMe()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
// Displays “WhatsmyName”
Console.WriteLine( " Parent Method Name {0} ", methodBase.Name );
}
12/5/2006 2:55 PM | FMAG

# re: Finding calling method using reflection

Gravatar Yeah, actually an even better way is to use Dynamic Proxy to do these kinds of things. 12/5/2006 8:34 PM | Mike

# re: Finding calling method using reflection

Gravatar Nice piece of code! really a interesting area to play around. Thanks 1/11/2008 1:56 PM | Murali

# re: Finding calling method using reflection

Gravatar asdasdasdasdasd 4/1/2008 8:26 AM | asda

# re: Finding calling method using reflection

Gravatar Here is the simple way to do it:

http://www.csharp-examples.net/reflection-calling-method-name/ 6/30/2008 7:18 PM | Ananda

Post a comment





 

Please add 7 and 8 and type the answer here:

 

 

Copyright © Mike Nichols