无法转换拉姆达前pression键入" ..."因为它不是委托类型 [英] Cannot convert lambda expression to type "..." because it is not a delegate type

查看:126
本文介绍了无法转换拉姆达前pression键入" ..."因为它不是委托类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!
我想写使用lambda前pressions这会从异步任务返回一个对象匿名方法。我想做到这一点在构造函数中,所以这是我不能让其父异步方法的原因。

Good day! I am trying to write an anonymous method using lambda expressions which would return an object from an async task. I would like to do this in the constructor, so that is the reason I can't make its parent method async.

该ReadJsonAsync方法返回一个会话对象。
我会告诉你相关的code:

The ReadJsonAsync method returns a Session object. I will show you the relevant code:

Session session;
fileService = new FileService();
session = async () =>  { return await fileService.ReadJsonAsync() };

在此先感谢!

推荐答案

如果你想要一个的匿名方法的,你必须要声明一个返回一个任务<会议&GT ; ,因为它被标记为它打上了异步修改,因此必须返回一个无效(仅适用于异步事件处理程序),工作任务< T>

If you want an Anonymous Method, you'll have to declare one which returns a Task<Session> as it is marked as it is marked with the async modifier, hence must return a void (only for async event handlers), Task or Task<T> :

Func<Task<Session>> anonFunction = async () => await fileService.ReadJsonAsync();

如果你要做的就是运行 ReadJsonAsync 一切,你也可以保存自己喜欢这样的状态机生成:

If all you do is run ReadJsonAsync, you may also save yourself the state machine generation like so:

Func<Task<Session>> anonFunction = fileService.ReadJsonAsync;

然后你可以等待它在一个高阶函数:

Func<Task<Session>> anonFunction = fileService.ReadJsonAsync;
await anonFunction();

这篇关于无法转换拉姆达前pression键入&QUOT; ...&QUOT;因为它不是委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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