public static void Main7() { CheckLengthOfString d = LessThanFive; d += MoreThanFive; d += ExactlyFive; //List<bool> results = new List<bool>(); //foreach (var del in d.GetInvocationList()) //{ // results.Add((bool)del.DynamicInvoke("Message")); // Console.WriteLine(string.Join(", ", results)); //} // List<bool> results = d.GetInvocationList().Select(del => (bool)del.DynamicInvoke("Message")).ToList(); GetLengths g = x => x.Length; g += x => x.Length + 2; g += x => x.Length + 1; List <bool> results = GottaCatchEmAll <bool>(d, "Message"); List <int> lengths = GottaCatchEmAll <int>(g, "Message"); Console.WriteLine(string.Join(", ", results)); Console.WriteLine(string.Join(", ", lengths)); Console.ReadKey(); }
static void Main() { // Chain methods in a Delegate CheckLengthOfString d = LessThanFive; d += MoreThanFive; d += ExactlyFive; // Invoke the delegate, no output and no return values were caught d("Message"); Console.WriteLine(new string('-', 40)); // Catch all results with a ForEach loop List <bool> results = new List <bool>(); foreach (var del in d.GetInvocationList()) { results.Add((bool)del.DynamicInvoke("Message")); } Console.WriteLine("Foreach loop results: " + string.Join(", ", results)); // Catch all results with a Lambda Expression Console.WriteLine(new string('-', 40)); List <bool> lambdaResults = d.GetInvocationList() .Select(del => (bool)del.DynamicInvoke("Message")) .ToList(); Console.WriteLine("LINQ Lambda Results: " + string.Join(", ", lambdaResults)); }
static void Main() { // Chain methods in a Delegate CheckLengthOfString d = LessThanFive; d += MoreThanFive; d += ExactlyFive; // Invoke the delegate, no output and no return values were caught d("Message"); Console.WriteLine(new string('-', 40)); List <bool> boolResults = CatchAllResults <bool>(d, "Message"); Console.WriteLine(string.Join(", ", boolResults)); GetLength p = x => x.Length; p += x => x.Length + 1; p += x => x.Length + 2; List <int> lengths = CatchAllResults <int>(p, "asd"); Console.WriteLine(string.Join(", ", lengths)); }
public static void Test() { #region Lecture1 Printer p = Print; p += Print; p += PrintTwice; p += Print; p += PrintTwice; p += Print; p -= Print; p("Message"); Delegate[] delegates = p.GetInvocationList(); foreach (var del in delegates) { //Console.WriteLine(del.Method); } #endregion Lecture1 #region Lecture2 CheckLengthOfString d = LessThanFive; d += MoreThanFive; d += ExactlyFive; /* Hard Way * List<bool> results = new List<bool>(); * * foreach(var del in d.GetInvocationList()) * { * results.Add((bool)del.DynamicInvoke("Message")); * } */ /* Static Way * List<bool> results = d.GetInvocationList().Select(del => (bool)del.DynamicInvoke("Message")).ToList(); */ /* Generic Way */ List <bool> results = GottaCatchEmAll <bool>(d, "Message"); Console.WriteLine(string.Join(", ", results)); GetLengths g = x => x.Length; g += x => x.Length + 1; g += x => x.Length + 2; List <int> lengths = GottaCatchEmAll <int>(g, "Message"); Console.WriteLine(string.Join(", ", g)); Console.WriteLine(d("Message")); #endregion Lecture2 #region Lecture 3 Action <string> printer = Print; printer += PrintTwice; printer("Message"); #endregion Lecture 3 }