Retrieving the calling method name from within a method [duplicate]

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

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

The compiler adds the caller’s name automatically; so:

void Foo() {
    PopularMethod();
}

will pass in "Foo".

Leave a Comment