【问题标题】:How to return current method name for async method [duplicate]如何返回异步方法的当前方法名称[重复]
【发布时间】:2021-02-09 16:50:25
【问题描述】:

我正在尝试将方法名称输出到文件标题 目前我的代码是

public async Task TestMethod()
{ 
    string fileName = testDirectory + "TC521_" + MethodBase.GetCurrentMethod().Name + "_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".txt";
}

但文件名返回为

Response_TC521_MoveNext_27-10-2020-11-52-17.txt

我希望它回来

Response_TC521_TestMethod_27-10-2020-11-52-17.txt

有什么想法吗?

【问题讨论】:

标签: c# asynchronous methods


【解决方案1】:

从另一个帖子中找到这个,这个有效

    public string GetCurrentMethod([CallerMemberName] string callerName = "")
    {
        return callerName;
    }

【讨论】:

  • 如果您在另一篇文章中发现此内容,我们将作为重复内容关闭 - 我们不会抄袭其他答案。
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 2012-11-16
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 2019-09-05
相关资源
最近更新 更多