Created
December 30, 2011 08:12
-
-
Save iburlakov/1538639 to your computer and use it in GitHub Desktop.
Determimes a caller of a method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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