如何将lambda表达式直接转换为对象?
我必须通过这样的操作来完成:
Action action = () => { ..// };
object o = action;
任何方法都可以做到这一点:
object o = () =>{}; //this doesn't compile
I have to do through Action like this:
Action action = () => { ..// };
object o = action;
any way to do this:
object o = () =>{}; //this doesn't compile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
虽然我真的不知道为什么你首先要把它存储为一个对象......
What about:
Though I don't really know why you'd want to store it as an object in the first place...
嗯,委托是对象,但 lambda 不是。
这个
object o = (Action)(() => {});
可以编译,但我不知道它看起来是否更好。Weeeell, delegates are objects, but lambdas aren't.
This
object o = (Action)(() => {});
will compile, but I don't know if it looks any better.另一种选择,并没有什么不同:
Another option, not all that different: