在 C# 中将 lambda 表达式或匿名函数转换为委托对象的最直接方法是什么?
我正在编写一些代码来解决正在处理的系统中的线程安全问题,推荐的方法之一是使用委托来解决跨线程问题。但出于某种原因,我不喜欢为我可能必须拦截的每个可能的操作定义委托,因此更喜欢使用匿名方法或 lambda 表达式,但编译器拒绝将其中任何一个强制转换为 System.委托对象。
这有捷径吗?
if (someListBox.InvokeRequired)
{
someListBox.Invoke(Some_System.Delegate_Object, new object[] {item});
}
否则
someListBox.Items.Add(item);
我想要像...
if (someListBox.InvokeRequired)
{
someListBox.Invoke((i) => { someListBox.Items.Add(i); }, new object[] {item});
}
否则
someListBox.Items.Add(item);
I am writing some code to solve thread-safe issues in a system am working on, and one of the recommended approaches is to use delegates to solve cross-thread issues. But for some reason, I don't like having to define a delegate for every possible operation i might have to intercept, and thus prefer working with anonymous methods or lambda expressions, but I the compiler refuses to cast any of these to a System.Delegate object.
Is there a shortcut to this?
if (someListBox.InvokeRequired)
{
someListBox.Invoke(Some_System.Delegate_Object, new object[] {item});
}
else
someListBox.Items.Add(item);
I want something like...
if (someListBox.InvokeRequired)
{
someListBox.Invoke((i) => { someListBox.Items.Add(i); }, new object[] {item});
}
else
someListBox.Items.Add(item);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题之一是编译器无法推断 lamda 的参数类型。即使对于给定的参数签名,也存在无限多个潜在的委托类型。所以你需要明确给出一个类型。如果您不关心参数名称,则
Action<...>
和Func<...>
是典型的候选者。我认为这应该有效:
或者以重构的形式以避免重复:
而且我认为您根本没有理由希望将
i
作为参数:One of your problems is that the compiler can't infer the parameter types of your lamda. And even for a given parameter signature there are infinitely many potential delegate types. So you need to explicitly give a type.
Action<...>
andFunc<...>
are typical candidates if you don't care about parameter names.I think this should work:
Or in refactored form to avoid repeating yourself:
And I see no reason why you'd want to have
i
as a parameter at all:您想要的方法的缺点是实际工作将在两个地方实施。一种替代方法可能如下所示:
话又说回来,您可以讨论此代码的执行频率。如果不是太多,也许最好通过不检查
InvokeRequired
来简化它,而是始终将调用包装在传递给BeginInvoke
的委托中:The downside with the approach that you want is that the actual work will be implemented in two places. One alternative approach might look like this:
Then again, you could debate how often this code is executed. If it is not extremely much, perhaps its better to simplify it a bit by not checking for
InvokeRequired
, but rather always wrap the call in a delegate passed toBeginInvoke
: