How to get method name win 8 app

懵懂的女人 提交于 2019-12-05 04:34:53

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();

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());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!