简体   繁体   English

如何将参数添加到 c# 中的异步 function 操作参数

[英]How to add parameters to an async function action parameter in c#

I have the method below我有下面的方法

protected async Task ProcessExecuteResponseAsync(string errorMessage, 
            Func<Task> failureAction = null)
{
    if (isOk == false)
    {
        if (failureAction != null)
        {
            var failureResult = failureAction.Invoke();
            await failureResult;
        }
    }
}

This works perfectly这完美地工作

However, now I want to pass error message to the failureAction action但是,现在我想将错误消息传递给 failureAction 动作

What is the syntax for that?它的语法是什么?

I tried我试过了

protected async Task ProcessExecuteResponseAsync(string errorMessage, 
            Func<Task, string> failureAction = null)
{
    if (isOk == false)
    {
        if (failureAction != null)
        {
            var failureResult = failureAction.Invoke(errorMessage);
            await failureResult;
        }
    }
}

But I get error that string cannot be converted to Task但我收到错误,字符串无法转换为任务

You have to change the order of the generic arguments from Func<Task, string> to Func<string, Task> .您必须将通用 arguments 的顺序从Func<Task, string>更改为Func<string, Task>

The last argument is always the return value and the ones before are the input arguments.最后一个参数始终是返回值,之前的参数是输入 arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM