博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#的Compiler Error CS1660
阅读量:6903 次
发布时间:2019-06-27

本文共 2006 字,大约阅读时间需要 6 分钟。

原文地址:

问题:

I am invoking a delegate and I'm not very informed about how it works and I have compilation errors because of it (Compiler Error CS1660). This is the code I have for it:

base.Invoke( delegate { bool flag = (((this.layerPickPlaceProcess != null) && (this.robotComponent != null)) && ((((StateEnum) this.layerPickPlaceProcess.State) == StateEnum.Idle) || (((StateEnum) this.layerPickPlaceProcess.State) == StateEnum.Ready))) && ((((StateEnum) this.robotComponent.State) == StateEnum.Idle) || (((StateEnum) this.robotComponent.State) == StateEnum.Ready)); this.cmdManualPlace.Enabled = flag; });

原因:

this is because Invoke accepts Delegate which is not (sic!) a delegate type as far as the C# compiler is concerned. A delegate type should define a call signature, while Delegate does not and is just a common ancestor. The expression delegate { ... } has type... try to guess... anonymous delegate(if it was a method it would be method group). They are not delegate types either! But they can be implicitly converted to a delegate type that has a matching signature. And delegate types can be implicitly converted to Delegate.

Action is: public delegate void Action();

simply, chains:

  • Anonymous method → Delegate: no conversion exists
  • Anonymous method → Action: implicit conversion if signature matches
  • Action → Delegate: implicit conversion (Action is descendant of Delegate)

Combine them:

  • Anonymous method → Action → Delegate: it works!
尝试以下代码即可解决:

// MethodInvoker is also acceptable. Action action = delegate { bool flag = (((this.layerPickPlaceProcess != null) && (this.robotComponent != null)) && ((((StateEnum) this.layerPickPlaceProcess.State) == StateEnum.Idle) || (((StateEnum) this.layerPickPlaceProcess.State) == StateEnum.Ready))) && ((((StateEnum) this.robotComponent.State) == StateEnum.Idle) || (((StateEnum) this.robotComponent.State) == StateEnum.Ready)); this.cmdManualPlace.Enabled = flag; }; base.Invoke(action);

转载于:https://www.cnblogs.com/hold/archive/2011/09/22/2286778.html

你可能感兴趣的文章
php 框架ci去index.php的方法
查看>>
Hyper-v学习(四),SMB虚拟机实时迁移
查看>>
基于spring3注解的google分页
查看>>
怎样利用Cocoapods模块化开发你的app
查看>>
NO.49 敏捷之旅2012年12月22日青岛站即将来袭。。。
查看>>
Android 页面全屏
查看>>
文字首行缩进
查看>>
麻省:第11课
查看>>
windows2008r2域信任排错一例
查看>>
OpenGrok安装配置
查看>>
Sublime Text 无法使用Package Control
查看>>
zookeeper简绍
查看>>
Linux系统Tomcat安全重启
查看>>
【翻译】写一个每秒接收 100 万数据包的程序究竟有多难?
查看>>
1、JDK与JRE的区分
查看>>
8、一个webpack项目的大体目录结构
查看>>
python计算熵、条件熵、信息增益以及信息增益比
查看>>
网站前端_Bootstrap排版/列表/表格/表单/按钮/图像1
查看>>
实用命令行工具详解—crontab
查看>>
code review
查看>>