匿名方法作为 BeginInvoke 的参数?
为什么不能将匿名方法作为参数传递给 BeginInvoke
方法? 我有以下代码:
private delegate void CfgMnMnuDlg(DIServer svr);
private void ConfigureMainMenu(DIServer server,)
{
MenuStrip mnMnu = PresenterView.MainMenu;
if (mnMnu.InvokeRequired)
{
mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu,
new object[] { server});
}
else
{
// Do actual work here
}
}
我试图避免声明代表。 为什么我不能写像下面这样的东西? 或者我可以,但我只是无法弄清楚正确的语法? 以下当前生成:
参数类型“匿名方法”不可分配给参数类型“System.Delegate”
好吧,当然是对的,但是我可以使用其他语法来执行此操作(避免必须声明单独的委托才能使用 BeginInvoke()
?
(能够做到这一点将完全符合使用匿名方法/lamda 代替显式委托的概念,而显式委托在其他地方都可以干净地工作。)
private void ConfigureMainMenu(DIServer server,)
{
MenuStrip mnMnu = PresenterView.MainMenu;
if (mnMnu.InvokeRequired)
{
mnMnu.BeginInvoke( // pass anonymous method instead ?
delegate(DIServer svr) { ConfigureMainMenu(server);},
new object[] { server});
}
else
{
// Do actual work here
}
}
Why can't you pass an anonymous method as a parameter to the BeginInvoke
method? I have the following code:
private delegate void CfgMnMnuDlg(DIServer svr);
private void ConfigureMainMenu(DIServer server,)
{
MenuStrip mnMnu = PresenterView.MainMenu;
if (mnMnu.InvokeRequired)
{
mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu,
new object[] { server});
}
else
{
// Do actual work here
}
}
I'm trying to avoid declaring the delegate. Why can't I write something like the below instead? Or can I, and I just can't figure out the correct syntax? The below currently generates an:
Argument type 'Anonymous method' is not assignable to parameter type 'System.Delegate'
Ok, that's right of course, but is there some other syntax I can use to do this (avoid having to declare a separate delegate in order to use BeginInvoke()
?
(Being able to do this would fit in neatly with the concept of using anon methods/lamdas in place of explicit delegates which works so cleanly everywhere else.)
private void ConfigureMainMenu(DIServer server,)
{
MenuStrip mnMnu = PresenterView.MainMenu;
if (mnMnu.InvokeRequired)
{
mnMnu.BeginInvoke( // pass anonymous method instead ?
delegate(DIServer svr) { ConfigureMainMenu(server);},
new object[] { server});
}
else
{
// Do actual work here
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试这个:
或者:
或者:
Try this:
Or:
Or:
你应该能够写出这样的东西:
You should be able to write something like this:
您可以编写一个扩展方法来包装匿名方法,甚至处理
InvokeRequired
语义:这将允许您执行以下操作:
You could write an extension method that would wrap anonymous methods, and even take care of the
InvokeRequired
semantics:This would allow you to do:
您可以通过调用自己来在单个方法中完成此操作:
...
You can do this in a single method by calling invoking yourself:
...
对于参数数量有限的完全匿名方法:
您可以根据需要使用其他 Func 委托类型之一。
For completely anonymous methods with a limited number of parameters:
You can use one of the other Func delegate types as needed.
我尝试了很多不同的方法,但没有一个有效。 即...
所以,简短的答案是否定的。 您可以在给定的上下文中创建简短的助手委托,并使用 lambda 使其变得更简洁,但仅此而已。
编辑:事实证明我错了。 下面的方法调用者答案有效。
请参阅此页面
I've tried a bunch of different methods but none work. ie...
So, the short answer is no. You could create short helper delegates in the given context and use lambdas to make it a bit neater but that's pretty much it.
EDIT: Turns out I'm wrong. The methodinvoker answer below works.
See this page