What is delegation? Are you familiar with delegation? The answer is yes! You must be familiar with delegation! You are familiar with delegation, just as you are familiar with a person standing upside down! Why? Because method calls are very familiar to anyone who has learned programming and cannot be familiar with them any more! So what is the relationship between method calling and delegation? In fact, the delegate is to change the passive call to the active service.
Delegation is actually a kind of thought change:From the previous passive call method to actively serve a method. The interactive thinking between methods has been transformed. If you have learned three layers, it is not difficult to imagine the call relationship between methods. What is the relationship between methods after the delegate is used? (Use two images to show them)
Figure 1: Delegate
Figure 2: delegation not used
I figured out what "delegation" is from a macro perspective. Use a piece of code to show more detailed aspects of delegation:
** Definition of delegation:
Observe the definition of the Delegate, it is not difficult to find that the delegate is the same as the class.
// Define the delegate Public Delegate void addeventhandler <t> (T); Public Delegate void deleteeventhandler <t> (T); Public Delegate void updateeventhandler <t> (t ); public Delegate void queryeventhandler <t> (T); Public Delegate t isexistseventhandler <t> (t );
** Delegated event:
Observe the definition of an event. It is not difficult to find that the event is the same as the method.
// Define the event public event addeventhandler <t> addevent; public event deleteeventhandler <t> deleteevent; public event updateeventhandler <t> updateevent; public event queryeventhandler <t> queryevent; public event isexistseventhandler <t> isexistsevent;
** Bind the method to the delegate:
+= new AddEventHandler<T>(this.manager.Add);+= new DeleteEventHandler<T>(this.manager.Delete);+= new UpdateEventHandler<T>(this.manager.Update);+= new QueryEventHandler<T>(this.manager.Query);+= new IsExistsEventHandler<T>(this.manager.isExist);
** Load Delegation for events:
this.AddEvent += new AddEventHandler<T>(this.manager.Add);this.DeleteEvent += new DeleteEventHandler<T>(this.manager.Delete);this.UpdateEvent += new UpdateEventHandler<T>(this.manager.Update);this.QueryEvent += new QueryEventHandler<T>(this.manager.Query);this.IsExistsEvent += new IsExistsEventHandler<T>(this.manager.isExist);<strong style="color: rgb(51, 51, 255);"></strong>
** The appearance Layer Code of the entire logon form:
Using system; using system. collections. generic; using system. LINQ; using system. text; using login. model. entity. base; using login. model. adapter; using login. model. entity; using system. reflection; namespace login. controller {// define delegate Public Delegate void addeventhandler <t> (T); Public Delegate void deleteeventhandler <t> (t ); public Delegate void updateeventhandler <t> (T); Public Delegate void queryeventhandler <t> (T); Public Delegate t isexistseventhandler <t> (t ); public class facademanager <t, tcollection> {private supermanager <t, tcollection> manager; // defines the event public event addeventhandler <t> addevent; public event deleteeventhandler <t> deleteevent; public event updateeventhandler <t> updateevent; public event queryeventhandler <t> queryevent; public event isexistseventhandler <t> isexistsevent; Public facademanager (t) {This. manager = This. createmanager (t); this. addevent + = new addeventhandler <t> (this. manager. add); this. deleteevent + = new deleteeventhandler <t> (this. manager. delete); this. updateevent + = new updateeventhandler <t> (this. manager. update); this. queryevent + = new queryeventhandler <t> (this. manager. query); this. isexistsevent + = new isexistseventhandler <t> (this. manager. isexist );} /// <summary> /// create the corresponding manager /// </Summary> /// <Param name = "T"> </param> /// <returns> </returns> Public supermanager <t, tcollection> createmanager (t) {string classname = typeof (t ). tostring () + "manager"; classname = classname. substring (classname. lastindexof ('. ') + 1); // return (supermanager <t>) assembly. load ("login "). createinstance ("login. controller. "+ classname); // parameter array object [] O = new object [1]; O [0] = T; Return (supermanager <t, tcollection>) assembly. load ("login "). createinstance ("login. controller. "+ classname, false, system. reflection. bindingflags. default, null, O, null, null );} /// <summary> /// determine whether the object exists /// </Summary> /// <Param name = "T"> </param> /// <returns> </returns> Public t isexist (T) {// return manager. isexist (t); return this. isexistsevent (t);} public bool add (t) {Throw new notimplementedexception ();} public bool Update (t) {Throw new notimplementedexception ();} public bool Delete (t) {Throw new notimplementedexception ();}}}
Summary:
What is the difficulty in the world? For this reason, it is easy for the hard person. If not, it is also difficult for the easy person. Is it easy for people to learn? If you do not learn it, it is easier for you to learn it.
Data room charging system cooperation edition (5)-initial recognition Commission