c. advanced entrusted content

1, Basic content

  1. What is delegation? What is the role of delegation? (omitted)
  2. Declaration of custom delegation: Public Delegate [Type] Mydel()
  3. Show delegates = = > anonymous delegates = = > lambda expressions (omitted)
  4. Built in delegation types: action < >, func < >, predicate < > (omitted)

2, Advanced content

  1. Multicast delegation
  • Add + / + = operator for multicast delegate.
  • Remove of multicast delegate - / - = operator.
  • The method of adding: Combine(), equivalent to + / + = is used;
  • Method of removal: Remove() is equivalent to - / - =;
  • Note: when the return type of a multicast delegate is not void, only the return value of the last called method can be obtained, and all previous methods will be discarded.

2. DynamicInvoke() method

Dynamically calls (late bound) all methods in the current delegate list. You can call all in turn, or you can specify to call one of them.

3. GetInvocationList() method

Gets the method in the delegate list and outputs it as an array. public virtual Delegate[ ] GetInvocationList ();    

Two methods, 2 and 3, can be used together. First, the method of array representation is obtained, and then the dynamic output methods are respectively obtained. See the example below.

 1 class Program
 2     {
 3         //Built in Action<>Delegate type, declaring a "Delegate instance MyAction"
 4         static void Main( string [ ] args )
 5         {
 6             Action<string>  MyAction = null;
 7             //Add two more methods to the MyFunc In the list, let MyFunc There are three ways to list
 8             MyAction += method1;
 9             MyAction += method2;
10             MyAction += method3;
11 
12             //Direct call MyFunc List, showing all methods in the list.
13             MyAction ("Prince of Mars");
14 
15             //The methods in the dynamic call list can be assigned separately;
16            
17             Delegate [ ] Arr = MyAction.GetInvocationList ();  //Get delegate list array first
18             Console.WriteLine ( "The delegation list contains[{0}]One way \n",Arr.Length );  //Number of methods in the list
19             //
20             Console.WriteLine ( "Delegation list MyActoin The method in is as follows:\n" ); //Output all methods in the list
21             foreach (Delegate  dd  in Arr)
22             {
23                 Console.WriteLine (dd.Method );
24             }
25             Console.WriteLine ();
26             //
27             //use DynamicInvoke()Method to dynamically call a single method
28             Arr [ 0 ].DynamicInvoke (" Zhang San ");
29             Arr [ 1].DynamicInvoke ( " Lucy " );
30             MyAction.GetInvocationList ()[2].DynamicInvoke(" Prince of Mars ");  //This is equal to Arr[2].DynamicInvoke();
31             
32             Console.ReadKey ();
33        }
34              
35       //Three methods of display declaration mehod1  method2 method3
36         public static void method1(string name)
37         {
38             Console.WriteLine ($"I'm Chinese. My name is{name},I would appreciate your comments.,The first method.");
39         }
40         //
41         public static void method2( string name )
42         {
43             Console.WriteLine ( $"I'm English. My name is{name},Please give me more advice, the second way." );
44         }
45         //
46         public static void method3( string name )
47         {
48             Console.WriteLine ( $"I'm Martian. My name is{name},Please give me more advice, the third way." );
49         }
50         }

4. GetMethodInfo() method -- the method namespace is: System.Reflection;

Gets the object of the method in the delegate list. (object: attribute, name, qualifier, etc.)

Here are four Attributes: Name, IsStatic, ReturnType and Attributes. There are four types.

 1  class Program
 2     {
 3         //Built in Action<>Delegate type, declaring a "Delegate instance MyAction"
 4         static void Main( string [ ] args )
 5         {
 6             Action<string>  MyAction = null;
 7             //Add two more methods to the MyFunc In the list, let MyFunc There are three ways to list
 8             MyAction += method1;
 9             MyAction += method2;
10             MyAction += method3;
11 
12             Delegate [ ] Arr = MyAction.GetInvocationList ();  //Get delegate list array
13              //If you do not use arrays, you can also write them completely: MyAction.GetInvocationList () [ 0 ].GetMethodInfo ().Name ;
14              //The code is concise and convenient, and is represented by an array. Take the first method in the list, whose index value is 0 ;
15              //
16             string ss = Arr [ 0 ].GetMethodInfo ().Name;          // The name of this method
17             Type tt  = Arr [ 0 ].GetMethodInfo ().ReturnType;  // Return type of this method
18             bool bb = Arr [ 0 ].GetMethodInfo ().IsStatic;         // Is this method static
19             MethodAttributes mm = Arr [ 0 ].GetMethodInfo ().Attributes;   //Get the property associated with this method, which is a MethodAttributes Enumeration values.
20 
21             Console.WriteLine ( $"The first method in the delegate list is{Arr [ 0 ].Method},\n\n The name is:{ss},  The return type is:{tt},  The static Boolean values are:{bb}. " );
22             Console.WriteLine ( $"\n The related properties of this method are:{mm}" );
23 
24             Console.ReadKey ();
25        }           
26       //Three methods of display declaration mehod1  method2 method3
27         public static void method1(string name)
28         {    Console.WriteLine ($"I'm Chinese. My name is{name},I would appreciate your comments.,The first method.");  }
29         //
30         public static void method2( string name )
31         {  Console.WriteLine ( $"I'm English. My name is{name},Please give me more advice, the second way." ); }
32         //
33         public static void method3( string name )
34         { Console.WriteLine ( $"I'm a Martian. My name is{name},Please give me more advice, the third way." ); }
35       }

Tags: C# Lambda Attribute

Posted on Tue, 17 Mar 2020 10:04:27 -0400 by ssj4gogita4