简体   繁体   中英

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

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> .

The last argument is always the return value and the ones before are the input arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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