将lambda异步到Expression< Func< Task>>. [英] Async lambda to Expression<Func<Task>>

查看:83
本文介绍了将lambda异步到Expression< Func< Task>>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,我可以将普通的lambda表达式转换为Expression<T>:

It is widely known that I can convert ordinary lambda expression to Expression<T>:

Func<int> foo1 = () => 0; // delegate compiles fine
Expression<Func<int>> foo2 = () => 0; // expression compiles fine

如何使用异步lambda进行相同操作?我尝试了以下类比:

How could I do the same with async lambda? I've tried the following analogy:

Func<Task<int>> bar1 = async () => 0; // also compiles (async lambda example)
Expression<Func<Task<int>>> bar2 = async () => 0; // CS1989: Async lambda expressions cannot be converted to expression trees

有什么解决方法吗?

推荐答案

C#仅在可以用表达式树表示代码的情况下,才能将lambda表达式转换为表达式树,如果您注意到,在其中没有等效的"async"关键字System.Linq.Expressions

C# can only convert lambda expression to Expression tree only if code can be represented by Expression Tree, if you notice, there is no equivalent of "async" keyword in Expressions in System.Linq.Expressions

因此,不仅异步,而且C#中任何在提供的Expressions中没有等效表达式的内容,C#都无法将其转换为Expression Tree.

So not only async, but anything in C# that has no equivalent expression in provided Expressions, C# can't convert it to Expression Tree.

其他示例是

  1. 锁定
  2. 不安全
  3. 使用

这篇关于将lambda异步到Expression&lt; Func&lt; Task&gt;&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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