![](/img/trans.png)
[英]c# - how to pass function as parameter (with parameters as values) without invoking it
[英]How to add parameters to an async function action parameter in c#
我有下面的方法
protected async Task ProcessExecuteResponseAsync(string errorMessage,
Func<Task> failureAction = null)
{
if (isOk == false)
{
if (failureAction != null)
{
var failureResult = failureAction.Invoke();
await failureResult;
}
}
}
這完美地工作
但是,現在我想將錯誤消息傳遞給 failureAction 動作
它的語法是什么?
我試過了
protected async Task ProcessExecuteResponseAsync(string errorMessage,
Func<Task, string> failureAction = null)
{
if (isOk == false)
{
if (failureAction != null)
{
var failureResult = failureAction.Invoke(errorMessage);
await failureResult;
}
}
}
但我收到錯誤,字符串無法轉換為任務
您必須將通用 arguments 的順序從Func<Task, string>
更改為Func<string, Task>
。
最后一個參數始終是返回值,之前的參數是輸入 arguments。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.