为什么此异步lambda函数调用无法在C#中编译? [英] Why does this async lambda function invocation not compile in C#?

查看:127
本文介绍了为什么此异步lambda函数调用无法在C#中编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的理解中,我似乎缺少了一些非常基本的东西.我正在尝试调用一个异步函数,该函数将采用调用方传递的Func<T>,以防异步函数在缓存中找不到值.我真的对为什么我的代码无法编译感到困惑.

I seem to be missing something really basic in my understanding. I am trying to call an async function that will take a Func<T> passed in by the caller, in case if the async function doesn't find a value in the cache. I am really puzzled as to why my code won't compile.

我的呼叫代码如下

static void Main(string[] args)
{
    Func<Task<string>> dataFetcher = async () =>
    {
        string myCacheValue = await new HttpClient().GetStringAsync("http://stackoverflow.com/");
        return myCacheValue;
    };

    MyCache<string> cache = new MyCache<string>();

    string mValue = cache.GetOrCreateAsync("myCacheKey", dataFetcher).Result;

}

MyCache如下

internal class MyCache<T> where T: class
{
    private Cache localCache = null;

    public MyCache()
    {
        localCache = new Cache();
    }
    public T GetOrCreate(string cacheKey, Func<T> doWork)
    {
        T cachedObject = null;
        if (string.IsNullOrEmpty(cacheKey) || string.IsNullOrWhiteSpace(cacheKey))
            return cachedObject;

        cachedObject = localCache.Get(cacheKey) as T;

        if (null == cachedObject)
        {
            cachedObject = doWork();
        }

        localCache.Add(cacheKey, cachedObject, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

        return cachedObject;
    }

    public async Task<T> GetOrCreateAsync(string cacheKey, Func<T> doWork)
    {
        T cachedObject = null;

        if (string.IsNullOrEmpty(cacheKey) || string.IsNullOrWhiteSpace(cacheKey))
            return cachedObject;

        try
        {
            cachedObject = await Task.Factory.StartNew<T>(doWork);
            localCache.Add(cacheKey, cachedObject, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
        }
        catch (Exception)
        {
            cachedObject = null;
        }
        finally
        {
        }

        return cachedObject;
    }
}

在调用代码行中

string mValue = cache.GetOrCreateAsync("myCacheKey", dataFetcher).Result;

给我一​​个编译时错误Argument 2: cannot convert from System.Func<System.Threading.Tasks.Task<string>> to System.Func<string>

gives me a compile time error Argument 2: cannot convert from System.Func<System.Threading.Tasks.Task<string>> to System.Func<string>

我迷失了什么?如果有人可以帮助,那就太好了!

I am at a loss as to what am I missing out on? If someone could help that will be great!

谢谢 〜KD

推荐答案

使用重载方法并将Func<Task<T>>参数传递给它.然后,您可以等待该结果(为Task<T>).

Use an overloaded method and pass a Func<Task<T>> argument to it. Then you can await on that result (which is a Task<T>).

public async Task<T> GetOrCreateAsync(string cacheKey, Func<Task<T>> doWorkAsync)
{
    T cachedObject = null;

    if (string.IsNullOrEmpty(cacheKey) || string.IsNullOrWhiteSpace(cacheKey))
        return cachedObject;

    try
    {
        cachedObject = await doWorkAsync();
        localCache.Add(cacheKey, cachedObject, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
    }
    catch (Exception)
    {
        cachedObject = null;
    }
    finally
    {
    }

    return cachedObject;
}

这篇关于为什么此异步lambda函数调用无法在C#中编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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