class Program { static void Main(string[] args) { Action<double> ops = MathOperations.Mutiply; ops += MathOperations.Squre; ops.Invoke(3); } }
public class MathOperations { public static void Mutiply(double value) { Console.WriteLine("result:{0}", value * 2); } public static void Squre(double value) { Console.WriteLine("result:{0}", Math.Pow(value, 2)); } }
改进的调用方式,防止多播中的末一个发生异常
class Program { static void Main(string[] args) { Action<double> ops = MathOperations.Mutiply; ops += MathOperations.Squre; //ops.Invoke(3); Delegate[] delegates = ops.GetInvocationList(); foreach (Action<double> d in delegates) { try { d(3); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
时间: 2024-10-08 23:59:26