Using GetInvocationList() Method in C# Delegates/Func() | Receiving Multiple Return Values from Delegates

2019年12月15日 3488点热度 0人点赞 1条评论
内容目录

In everyday use of delegates, the following common methods are available:

Method Name Description
 Clone   Creates a shallow copy of the delegate.
 GetInvocationList   Returns a list of calls for this multicast delegate in the order they will be invoked.
 GetMethodImpl   Returns the static method represented by the current MulticastDelegate.
 GetObjectData   Fills the SerializationInfo object with all the data needed to serialize this instance.
 MemberwiseClone   Creates a shallow copy of the current Object.
 RemoveImpl   Removes elements that are equal to the specified delegate from the invocation list.

Usage of GetInvocationList()

When a delegate has multiple return values

When you write a delegate or a Func<> generic delegate and bind multiple methods to the instance, each method will have a return value. This situation may arise:

  class Program
    {
        public static string a(string str)
        {
            Console.WriteLine("Method a");
            return str+"Method a";
        }
        public static string b(string str)
        {
            Console.WriteLine("Method b");
            return str + "Method b";
        }
        public static string c(string str)
        {
            Console.WriteLine("Method c");
            return str + "Method c";
        }
        static void Main(string[] args)
        {
            Func<string, string> func=a;
            func += b;
            func += c;
            Console.WriteLine(func("Test"));
            Console.ReadKey();
        }
}</span></pre>

 After invoking the delegate, only the return value of the last called method can be retrieved.


 

Using GetInvocationList()

GetInvocationList() can return the method chain of this delegate.

By using a loop, each method can be invoked in order, and the return value of the currently invoked method will be generated in each iteration.

    class Program
    {
        public static string a(string str)
        {
            Console.WriteLine("Method a");
            return str+"Method a";
        }
        public static string b(string str)
        {
            Console.WriteLine("Method b");
            return str + "Method b";
        }
        public static string c(string str)
        {
            Console.WriteLine("Method c");
            return str + "Method c";
        }
        static void Main(string[] args)
        {
            Func<string, string> func=a;
            func += b;
            func += c;
            var funclist = func.GetInvocationList();
            foreach (Func<string, string> f in funclist)
            {
                Console.WriteLine(f("Test"));
            }
            Console.ReadKey();
        }

  

 

This effectively separates the methods invoked in the delegate into a list, allowing for each to be called in a loop to retrieve their results.

痴者工良

高级程序员劝退师

文章评论