Reputation: 14787
Is there a way to programatically get the name of which method or property the current code is executing under?
void Test()
{
MessageBox.Show("This is a message from " + GetNameOfCurrentMethod);
}
I thought about throwing an exception, catching it and parsing the stack trace but there should be a better way to do this.
Upvotes: 2
Views: 1133
Reputation: 775
Not currently relevant but for future readers .NET 4.5 introduces a CallerMemberNameAttribute
which can be applied to optional method parameters to get the caller information (providing it doesn't get removed from Beta to RTM!). http://msdn.microsoft.com/en-us/library/hh534540(v=vs.110).aspx
Upvotes: 0
Reputation: 3129
Have you searched? C# how to get the name of the current method from code
Upvotes: 1