C# 中的匿名委托
我不可能是唯一一个厌倦了仅为一次需要委托的调用而定义和命名委托的人。 例如,我想以可能来自其他线程的形式调用 .Refresh() ,所以我编写了这段代码:
private void RefreshForm()
{
if (InvokeRequired)
Invoke(new InvokeDelegate(Refresh));
else
Refresh();
}
我什至不确定我是否必须这样做,我只是读了足够多的内容,担心它在某些情况下不起作用后期。
InvokeDelegate 实际上是在另一个文件中声明的,但是我真的需要一个专门用于此目的的整个委托吗? 根本就没有通用代表吗?
我的意思是,例如,有一个 Pen 类,但也有 Pens。pen-of-choice,所以你不必重新制作整个东西。 虽然不一样,但我希望你明白我的意思。
I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code:
private void RefreshForm()
{
if (InvokeRequired)
Invoke(new InvokeDelegate(Refresh));
else
Refresh();
}
I'm not even sure I have to, I just read enough to be scared that it won't work at some later stage.
InvokeDelegate is actually declared in another file, but do I really need an entire delegate dedicated just for this? aren't there any generic delegates at all?
I mean, for example, there's a Pen class, but there's also Pens.pen-of-choice so you don't have to remake the whole thing. It's not the same, but I hope you understand what I mean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的。 在 .NET 3.5 中,您可以使用 Func 和 操作 委托。 Func 委托返回一个值,而 Action 委托返回 void。 类型名称如下所示:
我不知道为什么它们每个都停在 4 个参数处,但这对我来说已经足够了。
Yes. In .NET 3.5 you can use Func and Action delegates. The Func delegates return a value, while Action delegates return void. Here is what the type names would look like:
I don't know why they stopped at 4 args each, but it has always been enough for me.
您可以使用 Action 委托,如下所示:
或者,使用 lambda 语法:
最后还有匿名委托语法:
There's the Action delegate you could use, like so:
Or, with lambda syntax:
Finally there's anonymous delegate syntax:
在这种特定情况下,您可以(并且应该)只使用 MethodInvoker 做到这一点...这就是它存在的原因。
如果您正在做其他事情,您可以像其他人回答的那样使用 Func或操作如果它们适合您的用例。
In this specific case you can (and should) just use MethodInvoker to do this... that is why it exists.
If you were doing something else you could, as others have answered use Func<T,...> or Action<T,...> if they fit your use case.
简短版本:
那么你也可以去掉InvokeRequired的检查; 你可以按原样称呼它。 如果您需要传递参数,也可以使用,因此不需要其他特定于参数的委托(也适用于无参数的 Action 委托):
Short version:
Then you can also drop the check of InvokeRequired; you can just call it as it is. Works also if you need to pass parameters, so there is no need for other parameter-specific delegates (works just as well with the parameter-less Action delegate as well):
定义您自己的委托确实可以使调试变得更容易,因为智能感知可以告诉您参数的名称。 例如,您可以这样编写委托:
当您使用它的代码时,.NET 将通知您参数名称、委托名称等,因此如果您不确定具体如何操作,委托定义中就会有很多上下文。某物被使用。
但是,如果您不介意牺牲智能感知,系统命名空间中已经定义了一类委托,可以用作临时委托:
仅
Action
和Action
存在于 .NET 2.0 中,但很容易声明一个辅助类,其中包含这些杂项临时函数所需的其余委托。Defining your own delegates can really make debugging easier, if only because Intellisense can tell you the names of your parameters. For example, you write a delegate like this:
When you use it code, .NET will inform you of the parameter names, delegate name, etc, so there's a lot of context right in the delegate definition if you aren't sure exactly how something is used.
However, if you don't mind sacrificing Intellisense, there is already a class of delegates definined in the System namespace which can be used as ad-hoc delegates:
Only
Action
andAction
exist in .NET 2.0, but its easy enough to declare a helper class with the remaining delegates you need for these kind of miscellaneous ad hoc functions.是的,有通用代表。
Action
是一个通用委托,它接受一些参数并且不返回任何值,Func
是一个接受一些参数并返回一个值的通用委托。Yes, there are generic delegates.
Action<T1, T2...>
is a generic delegate that takes some parameters and returns no value, andFunc<T1, T2...R>
is a generic delegate that takes some parameters and returns a value.