Directory
- The characteristics of the event.
- Message instances and how the compiler treats events
- Summarize
First, the incident
An event is a member of a class that presents a class with the ability to notify other objects of what has happened.
An event is a special kind of delegate. Other objects that conform to the rules of the method can register and unregister the event for concern.
Second, the Mail instance
Requirements: When a new message is received, the notification is forwarded to the fax machine and other machines so that they can process the new message in their own way
1, Mail information class: Bearer mail related information, inherit EventArgs
Public classNewmailinfo:eventargs {Private ReadOnly stringMsg from, to; Public stringMSG {Get{returnmsg;} } Public stringfrom {Get{return from; } } Public stringto {Get{returnto ;} } PublicNewmailinfo (string_from,string_to,string_msg) {msg=_msg; to=_to; from=_from; } }
2, Mail management: Responsible for receiving new mail, and notify the registration of other classes (such as: fax machines, printers, etc.)
First, define a method to process the new message, assign the information to the message hosting class, and invoke the notification method.
Public void Simulatenewmail (stringfromstring msg) { new Newmailinfo (from, to, msg); Onnewmail (e); }
Second, the new mail notification feature notifies other registered classes.
Event Eventhandler<newmailinfo> NewMail; protected virtual void Onnewmail (Newmailinfo e) {EventHandler <newmailinfo > temp = System.Threading.Interlocked.CompareExchange (ref NewMail, null , null ); if (temp! = null this , E); }
When the event keyword is detected by the compiler, it will:
Event EventHandler Msgevent;
translated into three parts :
1.
Null
- A delegate reference chain that stores the method reference for the final registration.
- Private delegate, private to prevent modification of the outer class.
- The Add () and remove () methods are later used to add and remove references to this delegate reference chain.
2.
Publicvoid Add_newmail (eventhandler<newmailinfo> value) { // current delegate chain eventhandler<newmailinfo> NewMail = this. _newmail; Eventhandler<newmailinfo> Prevhandler; do {prevhandler = newmail; Eventhandler<newmailinfo> Newhandler = (eventhandler<newmailinfo>) delegate.combine (PrevHandler, Value); NewMail = System.Threading.Interlocked.CompareExchange (ref this. _newmail, Newhandler, Prevhandler);} while (newmail! = prevhandler);}
3 . The Remove () method, similar to the Add function.
The entire mail management class can be implemented as such.
Public classMailmanager { Public EventEventhandler<newmailinfo>NewMail; protected Virtual voidOnnewmail (Newmailinfo e) {EventHandler<NewMailInfo> temp = System.Threading.Interlocked.CompareExchange (refNewMail,NULL,NULL); if(Temp! =NULL) Temp ( This, E); } Public voidSimulatenewmail (string from,stringTo,stringmsg) {Newmailinfo e=NewNewmailinfo ( from, to, MSG); Onnewmail (e); } }
View Code
3. How to register other classes
Public class Fax { public Fax (mailmanager mm) { mm. NewMail+ =faxmsg ; } Public void Faxmsg (object sender, Newmailinfo e) { Console.Write (e.msg+"") From the fax machine "");} }
The current use of + = to register, in fact, is also using the Add () method to register method reference.
Iii. Summary
- The compiler encounters an event this flag will be translated. A private delegate that outward-open registers add and unregisters the Remove method. Events can only be modified by the current class, and only the registration and logoff methods are available externally.
- The class that owns the event invokes the new method list maintained by the delegate to invoke the callback on the method, which, of course, has the ability to notify other objects.
- External classes can be notified by registering events, and are handled in their own way.
Event and wonder if you have that understanding too