【发布时间】:2017-02-09 21:26:37
【问题描述】:
我想通过使用 Reflection 等来获取此方法的名称...我使用了很多东西但我很累请帮助我。 如果该功能是同步的,那么它下面的代码将正常工作。请仔细阅读下面的代码,这将解决您的问题。
// this will work fine
public void Test()
{
// This GetCurrentMethod() will you the name of current method
string CurrentMethodName = GetCurrentMethod();
// output will be CurrentMethodName = Test
}
// this will not work
public async Task<int> GETNumber(long ID)
{
// This GetCurrentMethod() will you the name of current method if the method is sync or not async
string CurrentMethodName = GetCurrentMethod();
return await Task.Run(() => { return 20; });
}
此方法为我提供非异步方法的名称。但是我如何获得上述方法名称
> [MethodImpl(MethodImplOptions.NoInlining)]
> public static string GetCurrentMethod()
> {
> var stackTrace = new StackTrace();
> StackFrame stackFrame = stackTrace.GetFrame(1);
> return stackFrame.GetMethod().Name;
> }
但此方法仅适用于非异步方法。那么如何在c#中获取当前的异步方法名称
【问题讨论】:
-
这里的问题是,在您的特定情况下,使用
Task.Run的“堆栈”不是自然堆栈,因此已经启动了一个线程池线程来运行您的匿名方法,因此,堆栈不包含任何特定于GETNumber方法的内容。 但是,在这种特殊情况下,为您的匿名方法生成的名称包含该名称,该方法的名称将类似于.<GETNumber>b__1_0。 -
现在,问题是,你到底需要这个名字做什么?
-
副本仅对您的特定情况有效,但是,它涉及匿名方法或 lambda 表达式。如果您的异步方法是普通方法,则此方法不起作用。你确定
nameof(GETNumber)不会成功吗?
标签: c# asp.net asp.net-mvc c#-4.0 c#-3.0