Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get method name win 8 app

Tags:

c#

windows-8

How to get the current method name in win 8(WinRT) app ... earlier in wp7 we could use System.Reflection.MethodBase.GetCurrentMethod().Name but its not there anymore thanks

like image 222
CognitiveDesire Avatar asked Aug 22 '12 07:08

CognitiveDesire


2 Answers

Yes, .NETCore lacks a lot of such things... and don't even get me started on GetTypeInfo() ! But perhaps a pragmatic workaround is to get the compiler to do it for you?

string CallerName([CallerMemberName]string caller = "")
{
    return caller;
}
...
string name = CallerName();
like image 163
Marc Gravell Avatar answered Nov 11 '22 20:11

Marc Gravell


This option can be helpfull if you need to override a method

private string GetMethodName(Expression<Action> expression)
{
    var methodName = (expression.Body as MethodCallExpression).Method.Name;
    return methodName;
}

Then just call it like

GetMethodName(() => TheNameOfTheCallingMethod());
like image 39
Andrii Avatar answered Nov 11 '22 20:11

Andrii