Blog Stats
  • Posts - 62
  • Articles - 1
  • Comments - 56
  • Trackbacks - 40

 

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;

 

}

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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 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 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 Here is the simple way to do it:

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

# re: Finding calling method using reflection

Gravatar Very useful article for get calling methods using reflection 5/11/2009 8:32 AM | Dhiraj N. Jire

# re: Finding calling method using reflection

Gravatar Awesome information... and horrible information :) Ok, so no worries. I just need to change the way I handle this stuff. The learning will continue. 12/28/2009 6:41 AM | leather jackets

# re: Finding calling method using reflection

Gravatar Thank you for sharing such good experience.I also like to write such things in own blog. gucci sandals very good! 6/10/2010 11:07 PM | nikki

# re: Finding calling method using reflection

Gravatar Why is it always that stack traces never give enough information? 8/20/2010 10:54 AM | Vera Wang Mattress

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © Mike Nichols