C# 中的匿名委托

发布于 2024-07-23 05:22:46 字数 462 浏览 5 评论 0原文

我不可能是唯一一个厌倦了仅为一次需要委托的调用而定义和命名委托的人。 例如,我想以可能来自其他线程的形式调用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

输什么也不输骨气 2024-07-30 05:22:46

是的。 在 .NET 3.5 中,您可以使用 Func操作 委托。 Func 委托返回一个值,而 Action 委托返回 void。 类型名称如下所示:

System.Func<TReturn> // (no arg, with return value)
System.Func<T, TReturn> // (1 arg, with return value)
System.Func<T1, T2, TReturn> // (2 arg, with return value)
System.Func<T1, T2, T3, TReturn> // (3 arg, with return value)
System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value)

System.Action // (no arg, no return value)
System.Action<T> // (1 arg, no return value)
System.Action<T1, T2> // (2 arg, no return value)
System.Action<T1, T2, T3> // (3 arg, no return value)
System.Action<T1, T2, T3, T4> // (4 arg, no return value)

我不知道为什么它们每个都停在 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:

System.Func<TReturn> // (no arg, with return value)
System.Func<T, TReturn> // (1 arg, with return value)
System.Func<T1, T2, TReturn> // (2 arg, with return value)
System.Func<T1, T2, T3, TReturn> // (3 arg, with return value)
System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value)

System.Action // (no arg, no return value)
System.Action<T> // (1 arg, no return value)
System.Action<T1, T2> // (2 arg, no return value)
System.Action<T1, T2, T3> // (3 arg, no return value)
System.Action<T1, T2, T3, T4> // (4 arg, no return value)

I don't know why they stopped at 4 args each, but it has always been enough for me.

年华零落成诗 2024-07-30 05:22:46

您可以使用 Action 委托,如下所示:

private void RefreshForm()
{
    if (InvokeRequired) Invoke(new Action(Refresh));
    else Refresh();
}

或者,使用 lambda 语法:

private void RefreshForm()
{
    if (InvokeRequired) Invoke((Action)(() => Refresh()));
    else Refresh();
}

最后还有匿名委托语法:

private void RefreshForm()
{
    if (InvokeRequired) Invoke((Action)(delegate { Refresh(); }));
    else Refresh();
}

There's the Action delegate you could use, like so:

private void RefreshForm()
{
    if (InvokeRequired) Invoke(new Action(Refresh));
    else Refresh();
}

Or, with lambda syntax:

private void RefreshForm()
{
    if (InvokeRequired) Invoke((Action)(() => Refresh()));
    else Refresh();
}

Finally there's anonymous delegate syntax:

private void RefreshForm()
{
    if (InvokeRequired) Invoke((Action)(delegate { Refresh(); }));
    else Refresh();
}
阳光下慵懒的猫 2024-07-30 05:22:46

在这种特定情况下,您可以(并且应该)只使用 MethodInvoker 做到这一点...这就是它存在的原因。

if (InvokeRequired)
    Invoke(new MethodInvoker(Refresh));
else
    Refresh();

如果您正在做其他事情,您可以像其他人回答的那样使用 Func或操作如果它们适合您的用例。

In this specific case you can (and should) just use MethodInvoker to do this... that is why it exists.

if (InvokeRequired)
    Invoke(new MethodInvoker(Refresh));
else
    Refresh();

If you were doing something else you could, as others have answered use Func<T,...> or Action<T,...> if they fit your use case.

习ぎ惯性依靠 2024-07-30 05:22:46

简短版本:

Invoke((MethodInvoker)delegate { Refresh(); });

那么你也可以去掉InvokeRequired的检查; 你可以按原样称呼它。 如果您需要传递参数,也可以使用,因此不需要其他特定于参数的委托(也适用于无参数的 Action 委托):

private void SetControlText(Control ctl, string text)
{
    Invoke((MethodInvoker)delegate { ctl.Text = text; });
}

Short version:

Invoke((MethodInvoker)delegate { Refresh(); });

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):

private void SetControlText(Control ctl, string text)
{
    Invoke((MethodInvoker)delegate { ctl.Text = text; });
}
再可℃爱ぅ一点好了 2024-07-30 05:22:46

我真的需要整个代表吗
专门为此? 不在那儿
有通用代表吗?

定义您自己的委托确实可以使调试变得更容易,因为智能感知可以告诉您参数的名称。 例如,您可以这样编写委托:

public delegate int UpdateDelegate(int userID, string city, string, state, string zip);

当您使用它的代码时,.NET 将通知您参数名称、委托名称等,因此如果您不确定具体如何操作,委托定义中就会有很多上下文。某物被使用。

但是,如果您不介意牺牲智能感知,系统命名空间中已经定义了一类委托,可以用作临时委托:

Func<T>
Func<T, U>
Func<T, U, V>
Func<T, U, V, W>
Action, Action<T>
Action<T, U>
Action<T, U, V>
Action<T, U, V, W>

ActionAction 存在于 .NET 2.0 中,但很容易声明一个辅助类,其中包含这些杂项临时函数所需的其余委托。

Do I really need an entire delegate
dedicated just for this? aren't there
any generic delegates at all?

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:

public delegate int UpdateDelegate(int userID, string city, string, state, string zip);

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:

Func<T>
Func<T, U>
Func<T, U, V>
Func<T, U, V, W>
Action, Action<T>
Action<T, U>
Action<T, U, V>
Action<T, U, V, W>

Only Action and Action 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.

仅冇旳回忆 2024-07-30 05:22:46

是的,有通用代表。 Action 是一个通用委托,它接受一些参数并且不返回任何值,Func 是一个接受一些参数并返回一个值的通用委托。

Yes, there are generic delegates. Action<T1, T2...> is a generic delegate that takes some parameters and returns no value, and Func<T1, T2...R> is a generic delegate that takes some parameters and returns a value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文