public void TestMulticast() { BinaryOp d = BinaryAnd; //you can skip the new init d += BinaryOr; //add more method var delegateRes = d(2, 7); //will call all methods in the list Console.WriteLine(delegateRes); //but last will be as a result //call all methods dirrectly foreach (var method in d.GetInvocationList()) { Console.WriteLine(method.DynamicInvoke(2, 7)); } //unsubscribe d -= BinaryOr; d -= BinaryAnd; if (d != null) { d(5, 6); //will newer call, all unsubscribed, delegate empty } }
static void Main(string[] args) { Console.WriteLine("Delegate Example!!!!!!!"); BinaryOp b = new BinaryOp(SimpleMath.Sub); b += SimpleMath.Add; Console.WriteLine("10+10 = {0}", b(10, 10)); b.GetInvocationList(); foreach (Delegate d in b.GetInvocationList()) { Console.WriteLine("Method Name: {0}", d.Method); Console.WriteLine("Type name: {0}", d.Target); } Console.Read(); }
public static void DisplayDelegateInfo(BinaryOp delegateObject) { foreach (Delegate d in delegateObject.GetInvocationList()) { Console.WriteLine($"Method name: {d.Method}"); Console.WriteLine($"Target type: {d.Target?.GetType()}"); } }
static void UseDelegate() { BinaryOp op = new BinaryOp(Add); foreach (var v in op.GetInvocationList()) { Console.WriteLine("Method {0}", v.Method); Console.WriteLine("GetType {0}", v.GetType()); Console.WriteLine("Target {0}", v.Target); } Console.WriteLine(); op += Sub; foreach (var v in op.GetInvocationList()) { Console.WriteLine("Method {0}", v.Method); Console.WriteLine("GetType {0}", v.GetType()); Console.WriteLine("Target {0}", v.Target); } Console.WriteLine(); op -= Add; foreach (var v in op.GetInvocationList()) { Console.WriteLine("Method {0}", v.Method); Console.WriteLine("GetType {0}", v.GetType()); Console.WriteLine("Target {0}", v.Target); } Console.WriteLine(); foreach (var v in op.GetInvocationList()) { Console.WriteLine("Method {0}", ((BinaryOp)v)(10, 100)); } Console.WriteLine(); }
void DoProggy() { callBack = Add; callBack += Sub; foreach (Delegate d in callBack.GetInvocationList()) { Console.WriteLine("Method Name[" + d.Method + "]"); Console.WriteLine("Type Name[" + d.Target + "]"); } // do the responsible thing :) if(callBack != null) { callBack(4, 5); } }
static void Main(string[] args) { #region Объявление делегатов //Sum(10, 20); //Mul(1, 10); //сигнатуры методов: void (int a, int b) --> сигнатура делегата /*BinaryOp op = new BinaryOp(Sum); * op += new BinaryOp(Sum); * op += Sum; * op += Mul; * op(1, 10); * Console.WriteLine(int.MaxValue);*/ /*Action<int, int> action = Sum; * action(10, 20); * * Func<DateTime, string> func = ToString; * Console.WriteLine( func(DateTime.Now));*/ #endregion Func <int, int, int> workItem = GetNumbers; workItem.BeginInvoke(10, 20, ShowGetNumbersResult, null); //c использованием Колл-Бэка while (Console.ReadLine() != "exit") { } BinaryOp op = new BinaryOp(Sum); op += Mul; op += Sum; Delegate[] ops = op.GetInvocationList(); for (int i = 0; i < ops.Length; i++) { (ops[i] as BinaryOp).BeginInvoke(1, 10, null, null); } }
private static void ExploreDelegates() { // Create a BinaryOp delegate object that // "points to" SimpleMath.Add(). SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); // Invoke Add() method indirectly using delegate object. Console.WriteLine("10 + 10 is {0}", b(10, 10)); Console.WriteLine(); // Print the names of each member in the // delegate's invocation list. foreach (Delegate d in b.GetInvocationList()) { Console.WriteLine("Method Name: {0}", d.Method); Console.WriteLine("Type Name: {0}", d.Target); } }
static void Main(string[] args) { #region Объявление делегатов //Sum(10, 20); //Mul(1, 10); //сигнатуры методов: void (int a, int b) --> сигнатура делегата /*BinaryOp op = new BinaryOp(Sum); op += new BinaryOp(Sum); op += Sum; op += Mul; op(1, 10); Console.WriteLine(int.MaxValue);*/ /*Action<int, int> action = Sum; action(10, 20); Func<DateTime, string> func = ToString; Console.WriteLine( func(DateTime.Now));*/ #endregion Func<int, int, int> workItem = GetNumbers; workItem.BeginInvoke(10, 20, ShowGetNumbersResult, null); //c использованием Колл-Бэка while (Console.ReadLine() != "exit"){ } BinaryOp op = new BinaryOp(Sum); op += Mul; op += Sum; Delegate[] ops = op.GetInvocationList(); for (int i = 0; i < ops.Length; i++) { (ops[i] as BinaryOp).BeginInvoke(1, 10, null, null); } }