如何实现异步操作的委托方法? [英] How do you implement an async action delegate method?

查看:130
本文介绍了如何实现异步操作的委托方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一点背景资料。

我学习的网络API堆栈,我试图在封装等参数成功和错误codeS一个结果对象的形式的所有数据。

不同的方法然而,会产生不同的结果和错误codeS但结果对象一般会被实例相同的方式。

要节省一些时间,也更多地了解异步/等待在C#中的能力,我想换我的网络API操作的所有方法体中的异步操作的委托,但碰上有点暴牙的..

考虑以下类:

 公共类结果
{
    公共BOOL成功{搞定;组; }
    公开名单< INT>错误$ C $ {CS获得;组; }
}公共异步任务<结果> GetResultAsync()
{
    返回等待DoSomethingAsync<结果>(结果=>
    {
        //这里做什么
        result.Success =真;        如果(SomethingIsTrue)
        {
            result.Error codes.Add(404);
            result.Success = FALSE;
        }
    }
}

我想写一个执行结果的对象上的操作的方法和返回。通常情况下,通过同步方法将是

 公共牛逼DoSomethingAsync< T>(动作< T> resultBody)其中T:因此,新的()
{
    ŧ结果=新T();
    resultBody(结果);
    返回结果;
}

但我怎么改变这个方法为异步方法使用异步/的await?

这是我曾尝试:

 公共异步任务< T> DoSomethingAsync< T>(动作< T,任务> resultBody)
    其中T:结果,新的()
{
    //但我不知道什么从这里做。
    //什么我等待?
}


解决方案

行动1所述的异步同等学历; T> Func键< T,任务> ,所以我相信这是你要找的内容:

 公共异步任务< T> DoSomethingAsync< T>(Func键< T,任务> resultBody)
    其中T:结果,新的()
{
  ŧ结果=新T();
  等待resultBody(结果);
  返回结果;
}

A little background information.

I am learning the Web API stack and I am trying to encapsulate all data in the form of a "Result" object with parameters such as Success and ErrorCodes.

Different methods however, would produce different results and error codes but the result object would generally be instantiated the same way.

To save some time and also to learn more about async/await capabilities in C#, I am trying to wrap all the method bodies of my web api actions in an asynchronous action delegate but got caught in a bit of a snag...

Given the following classes:

public class Result
{
    public bool Success { get; set; }
    public List<int> ErrorCodes{ get; set; }
}

public async Task<Result> GetResultAsync()
{
    return await DoSomethingAsync<Result>(result =>
    {
        // Do something here
        result.Success = true;

        if (SomethingIsTrue)
        {
            result.ErrorCodes.Add(404);
            result.Success = false;
        }
    }
}

I want to write a method that performs an action on a Result object and return it. Normally through synchronous methods it would be

public T DoSomethingAsync<T>(Action<T> resultBody) where T : Result, new()
{
    T result = new T();
    resultBody(result);
    return result;
}

But how do I transform this method into an asynchronous method using async/await?

This is what I have tried:

public async Task<T> DoSomethingAsync<T>(Action<T, Task> resultBody) 
    where T: Result, new()
{
    // But I don't know what do do from here.
    // What do I await?
}

解决方案

The async equivalent of Action<T> is Func<T, Task>, so I believe this is what you're looking for:

public async Task<T> DoSomethingAsync<T>(Func<T, Task> resultBody)
    where T : Result, new()
{
  T result = new T();
  await resultBody(result);
  return result;
}

这篇关于如何实现异步操作的委托方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆