* 1.MethodInvoke和Action都是方法返回类型为空的委托.
* 2.MethodInvoker的原型定义为---> public delegate void MethodInvoker();
Delegate d1 = (MethodInvoker)delegate { };
//Delegate d2 = (MethodInvoker)(() => { });
//Delegate d3 = (Action)delegate { };
//Delegate d4 = (Action)(() => { });
对于Control.Invoke()来说,任何的代理类型都是可接受的,也就是说ThreadStart和MethodInvoker都是可以接受的类型。这样编译器反而不知道应该用哪个代理去匹配匿名函数了,导致了编译错误的发生。
知道了原因,问题就很容易解决了。我们只需要加上MethodInvoker这个wrapper就能使用匿名函数了。
Invoke(new MethodInvoker(delegate { Text = “test”; }));
或者更简单地,用Lambda表达式来解决问题:
Invoke(new MethodInvoker(() => Text = “test”));