Skip to content

Instantly share code, notes, and snippets.

@iburlakov
Created December 30, 2011 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iburlakov/1538639 to your computer and use it in GitHub Desktop.
Save iburlakov/1538639 to your computer and use it in GitHub Desktop.
Determimes a caller of a method
public class Foo
{
public void Do(int bar, string baz)
{
Console.WriteLine(GetCallerMethodName());
}
public void Do(int bar)
{
this.Do(bar, null);
}
public void Do(string baz)
{
this.Do(0, baz);
}
private string GetCallerMethodName()
{
StackTrace trace = new StackTrace();
int frameIndex = 1;
while (frameIndex < trace.FrameCount)
{
MethodBase callerMethod = trace.GetFrame(frameIndex).GetMethod();
if (callerMethod.DeclaringType.FullName != typeof(Foo).FullName)
{
return string.Format("{0}.{1}", callerMethod.DeclaringType.FullName, callerMethod.Name);
}
frameIndex++;
}
return string.Empty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment