有时我听不懂最简单的东西,我确定它在我的脸上,我只是看不到它。我试图在这个简单的类中为方法创建委托(delegate):public static class BalloonTip{ public static BalloonType BalType { get; set; } public static void ShowBalloon(string message, BalloonType bType) { // notify user }}现在,该Action 应该创建代理,而实际上没有使用关键字“delegate”声明一个代理,我是否正确理解?然后:private void NotifyUser(string message, BalloonTip.BalloonType ballType) { Action<string, BalloonTip.BalloonType> act; act((message, ballType) => BalloonTip.ShowBalloon(message, ballType)); }这无法编译。为什么?(顺便说一句,我需要此委托(delegate)而不是直接调用ShowBalloon()的原因是,调用必须从不同于UI的另一个线程进行,所以我认为我需要Action )谢谢, (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您需要首先将匿名方法分配给Action变量,然后使用传递给该方法的参数来调用它:private void NotifyUser(string message, BalloonTip.BalloonType ballType){ Action<string, BalloonTip.BalloonType> act = (m, b) => BalloonTip.ShowBalloon(m, b); act(message, ballType);}在这种情况下,由于Action变量所需的参数与封装方法的参数相同,因此您也可以直接引用该方法:private void NotifyUser(string message, BalloonTip.BalloonType ballType){ Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon; act(message, ballType);}关于c# - Action <>多参数语法说明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11172089/ (adsbygoogle = window.adsbygoogle || []).push({});
10-08 23:21