【发布时间】:2012-10-25 18:30:28
【问题描述】:
如何在 win 8(WinRT) 应用程序中获取当前方法名称...在 wp7 早期我们可以使用 System.Reflection.MethodBase.GetCurrentMethod().Name 但它不再存在了,谢谢
【问题讨论】:
-
您是指在 WinRT 中吗?
GetCurrentMethod肯定在 .NET 4.5..
如何在 win 8(WinRT) 应用程序中获取当前方法名称...在 wp7 早期我们可以使用 System.Reflection.MethodBase.GetCurrentMethod().Name 但它不再存在了,谢谢
【问题讨论】:
GetCurrentMethod 肯定在 .NET 4.5..
是的,.NETCore 缺少很多这样的东西......甚至不要让我在GetTypeInfo() 上开始!但也许一个实用的解决方法是让编译器为你做这件事?
string CallerName([CallerMemberName]string caller = "")
{
return caller;
}
...
string name = CallerName();
【讨论】:
如果您需要覆盖方法,此选项会很有帮助
private string GetMethodName(Expression<Action> expression)
{
var methodName = (expression.Body as MethodCallExpression).Method.Name;
return methodName;
}
那就这样称呼吧
GetMethodName(() => TheNameOfTheCallingMethod());
【讨论】: