C# Action<> 多参数语法说明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11172089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:52:14  来源:igfitidea点击:

Action<> multiple parameters syntax clarification

c#actionnotifyicon

提问by Amc_rtty

Sometimes I can't understand the simplest things, i'm sure it's in my face, i just fail to see it. Im trying to create a delegate for a method in this simple class:

有时我无法理解最简单的事情,我确信它在我的脸上,我只是看不到它。我试图在这个简单的类中为一个方法创建一个委托:

public static class BalloonTip
{
    public static BalloonType BalType
    { 
        get; 
        set; 
    }

    public static void ShowBalloon(string message, BalloonType bType)
    {
        // notify user
    }
}

Now, this Action<> is supposed to create the delegate without actually declaring one with the keyword "delegate", did I understand correctly? Then:

现在,这个 Action<> 应该创建委托而不用实际声明一个关键字“delegate”,我理解正确吗?然后:

private void NotifyUser(string message, BalloonTip.BalloonType ballType)
    {
        Action<string, BalloonTip.BalloonType> act; 
        act((message, ballType) => BalloonTip.ShowBalloon(message,  ballType));
    }

This fails to compile. Why?

这无法编译。为什么?

(By the way, the reason why I need this delegate instead of directly calling ShowBalloon(), is that the calls must be made from another thread than the UI one, so I figured I need the Action<>)

(顺便说一下,我需要这个委托而不是直接调用 ShowBalloon() 的原因是调用必须从另一个线程而不是 UI 线程进行,所以我想我需要 Action<>)

Thanks,

谢谢,

采纳答案by Douglas

You need to first assign your anonymous method to the Actionvariable, then invoke it with the arguments passed in to the method:

您需要首先将匿名方法分配给Action变量,然后使用传递给方法的参数调用它:

private void NotifyUser(string message, BalloonTip.BalloonType ballType)
{
    Action<string, BalloonTip.BalloonType> act = 
        (m, b) => BalloonTip.ShowBalloon(m, b);

    act(message, ballType);
}

In this case, since the arguments expected by your Actionvariable are identical to those of the encapsulated method, you may also reference the method directly:

在这种情况下,由于您的Action变量期望的参数与封装方法的参数相同,您也可以直接引用该方法:

private void NotifyUser(string message, BalloonTip.BalloonType ballType)
{
    Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon;

    act(message, ballType);
}

回答by Marcelo Zabani

Shouldn't you assign to the actvariable? Something in the lines of:

你不应该分配给act变量吗?在以下方面的东西:

Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon;

Not only did you not assign a method to act, as it seems you are trying to invoke act passing it a anonymous method as a parameter, while it receives a string and a BalloonTip.BalloonType.
In the end, you should return act, and thus your method to get a delegate to the notification method should be:

您不仅没有将方法分配给act,因为您似乎试图调用将匿名方法作为参数传递给它的行为,同时它接收一个字符串和一个 BalloonTip.BalloonType。
最后,您应该 return act,因此您获取通知方法委托的方法应该是:

public Action<string, BalloonTip.BalloonType> GetNotificationMethod() {
   Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon;
   return act;
}  

You can also make it simpler:

你也可以让它更简单:

public Action<string, BalloonTip.BalloonType> GetNotificationMethod() {
   return BalloonTip.ShowBalloon;
}  

Hope I understood your question ok. Good luck.

希望我能理解你的问题。祝你好运。