Analytical commissioned IL ----- packing logic and code reuse

Analytical commissioned IL ----- packing logic and code reuse

1. The nature of the commission

Entrust everyone is familiar with it, we often are exposed to or used. LINQ query is based on the principal to achieve.

We often define this delegate: public delegate void SayHiDelegate (string name);

So what is the nature of the commission is it?

Commissioned before the introduction of the essence, we look first to understand IL, IL intermediate language is an intermediate language code that we wrote in VS and computer binary code. Our code compiled IL, IL GIT final compiled by computer binary code into a computer can recognize. IL can see the real native C # code, when we discuss a number of technical depth, IL is a good helper, you can see the real code.

We all know that our C # code can be compiled to see the implementation code (such as decompiler decompile tool ILSpy )

ILSpy can choose to see VS C # code written or IL intermediate code, as shown below:

 

 When we choose to IL view, you can see each teacher says any type inherits from the Object object, as shown below;

 

We can also find the class constructor is tired of .ctor identified,

 We view the delegate found delegate inherited from multicast delegate MulticastDelegate .

From here I can understand what the nature of the commission , commission is in fact a class .

2. entrust the role of

Is not it call the delegate method? We directly through method calls can be achieved? Why should we spend commissioned it?

Mainly because it has commissioned a number of benefits:

① logic decoupling, remained stable: our commission incoming parameter is a method, a way is a logical structure, commissioned by us, you can be wrapped logical method

② code reuse can be done to ensure the consistency of the project, facilitating the centralized management code: Different people have different coding habits, we can achieve a common part of the package pulled by the commission, perform partial separation alone.

I use this simple little program commissioned achieve, we can refer to look

a using System;
 a using System.Collections.Generic;
 a using System.Linq;
 a using System.Text; 

namespace Theme.Delegate 
{ 
    public  delegate  void SayHiDelegate ( String name); // essence delegate is a class that inherits multicast delegate class (the MulticastDelegate) 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // each of the methods is a logical, we achieved by coupling a discrete logic delegate the method as an argument, equivalent 
            Say ( " dawn " , SayHiChiness); 
            Say ( " James" , SayHiAmerica); 
            Say ( " Landes " , SayHiJapan); 
            Say ( " kloer " , SayHiKorea); 
            Console.ReadLine (); 
        } 
        public  static  void Say ( String name, SayHiDelegate Method) 
        { 
            Console.WriteLine ( " before .. .. " ); // analog common code portions multiplexing 
            Method (name); // equivalent to Method.invoke (name); 
            Console.WriteLine ( " After .... " ); // analog common code reuse section
        }
        public static void SayHiChiness(string name)
        {
            Console.WriteLine("你好,欢迎您:"+name);
        }
        public static void SayHiJapan(string name)
        {
            Console.WriteLine("SHJDH56%^%^S%A^:"+name);
        }
        public static void SayHiAmerica(string name)
        {
            Console.WriteLine("hi,Welcome:" + name);
        }
        public static void SayHiKorea(string name)
        {
            Console.WriteLine("#$SYUD^&&S^&HSD&:" + name);
        }
        
    }
    
}

 

In the above code, I can be encapsulated by the method SayHiDelegate of SayHi various ways. If you need to expand in the future sayhi method, we can not specify a logical method sayhi directly when calling.

After using delegates, the public part of the equivalent pulled, packaging, the purpose of code reuse, which put different parts delegate callbacks.

 

Guess you like

Origin www.cnblogs.com/zxwDont/p/11493251.html