Experience the reality and the illusory
- A .NET event mechanism instance
Today, payday. Just thirty days in the past, I gave the company to the company, with the company as home, let the newlywed wife are unique, now I have to buy something for her, accompany her movie, eat a meal What, good compensation. Who knows when you get off work, a sunny day is a tear. The company has issued a notice on the high-rise opening of the company, telling us this development group, because the project is postponed, the salary and bonuses in the last month are not collected. Originally, the brothers are expected today, but now .... Hey, what should I do? Darkness or drainage, 言 提 还 还 直 是 还 是 还 难 难 难 难 难 难 难In the attitude of this matter, the team members have brought differences. Part is considered that you should find a representative and company negotiation to maintain our rights; another part is considered to be unhappy with the company, or "Been to Dumble, Rice" is the best.
Due to some factors (project extension), a type (company) has taken one thing (buckle and salary and bonus), and others have adopted some actions after learning this. Rights and anti-weight weights).
There is an event model in the CLR to simulate the above events. Event is very useful in computer programs, and can even be said to be a place and must not be outside.
Event production
In order to complete the type of event, what we need is:
1, a type 2 of the event-related information, a delegate type and an event 3, in one way, "event" has occurred 4, a method of issuing an event has occurred, I call it "alarm"
The above "things" are located in the SoftwareComplany type.
1. The event information type should inherit from the System.Eventargs type, in naming style, it is recommended to add Eventargs at the end of this type name, indicating that the type is an event information type.
public class DecisionMakingEventArgs: System.EventArgs {private readonly System.String causation, result; public DecisionMakingEventArgs (System.String causation, System.String result) {this.causation = causation; this.result = result;}}
2. Naming of the principal type, it is recommended to end with EventHandler. This type has two parameters: objects that make event notifications, event information types; and event is actually an instance of the commission.
Public Delegate void DecisionMak Inventage (System.Object Source, DecisionMakingeventargs Meventargs); Public Event DecisionMakingEventHandler DMMSG;
3. In the following method, "event" happened.
Public void conference (system.string color) {decisionMakingeventargs e = new decisionMakingeventArgs (causation, result); // next meeting decision onDmmsg (e);}
4, "alarm" method. In this method, first check if there is a type "subscription". If there is, the type of "subscription" event is notified. Private Void OnDMMSG (DecisionMakingeventargs E) {// View Is there a type "subscription" of the event if (DMMSG! = null) {// Warning DMMSG (this, e)}}
Event subscription
The Programmeremployee type and the FinanceemPloyee type "Subscribe" DMMSG event, but on the "attitude" of the event, the two types are completely different.
Lazy, proud, suspected that all programmers should maintain their rights, fight with the company, if the company is "aggressive", that is not a jade, not awkward; diligent, humble, always believe in the upper level, but "laugh" The financial personnel of the flowers "must review themselves and let the company" slaughter ". Hey, woman, your name is a weak.
public class ProgrammerEmployee {public ProgrammerEmployee (SoftwareComplany sc) {sc DmMsg = new SoftwareComplany (PEMsg);.} private void PEMsg (System.Object source, DecisionMakingEventArgs e) {System.Console.WriteLine ( "Top Hulu meat meal hunger, Smile talking about rings, ");}}
Public Class FinanceemPloyee {Public FinanceemPloyee (SoftwareComplany SC) {Sc. Dmmsg = New SoftwareComplany (femsg);
Private void femsg (system.object source, decisionmakingeventargs e) {system.console.writeline ("New Makeup, Bright, Shame");}}
Complete code
// Company type public class SoftwareComplany {// event information type public class DecisionMakingEventArgs: System.EventArgs {// relevant information fields private readonly System.String causation, result; public DecisionMakingEventArgs (System.String causation, System.String result) {this .causation = causation; this.result = result;} // delegate prototype public delegate void DecisionMakingEventHandler (System.Object source, DecisionMakingEventArgs mEventArgs); // define event public event DecisionMakingEventHandler DmMsg; // // monthly meetings the company system is not perfect Therefore never Minutes recording time public void Conference (System.String causation, System.String result) {DecisionMakingEventArgs e = new DecisionMakingEventArgs (causation, result); // alarm OnDmMsg (e);} private void OnDmMsg (DecisionMakingEventArgs e ) {// check if there is a type "subscription", the event IF (DMMSG! = Null) {// Warning DMMSG (this, e)}}}}
Public Class Programmeremployee {Public Programmeremployee (SoftwareComplany SC) {Sc. DMMSG = New SoftwareComplany (PEMSG);
Private Void Pemsg (System.Object Source, DecisionMakingEventArgs E) {// Harness can't be "eat their meat, sleep" SYSTEM.CONSOLE.WRITELINE ("Zhuangzhi Huuni Wi-meal, smile and rustic, Huxiongnone");}}
Public Class FinanceemPloyee {Public FinanceemPloyee (SoftwareComplany SC) {Sc. Dmmsg = New SoftwareComplany (femsg);
Private void femsg (system.object Source, DecisionMakingeventargs E) {// Sample System.Console.Writeline ("New Makeup, Bright Scense, Shi Retrieval");}}
public class app {public static void Main (System.String [] args) {SoftwareComplany sc = new SoftwareComplany (); ProgrammerEmployee pe = new ProgrammerEmployee (); FinanceEmployee fe = new FinanceEmployee (); // meeting sc Conference ( "Project. Extension "," buckle salary ")}}}
There are too many people in the world who are getting delicacy. In the project development, almost every person is like this. On the one hand, it is hoped that customers can fully understand their needs, and on the other hand, they hope that their programs can "do more with less" (I don't know if the popular advertisement is allegedly infringed). Of course, we must not only have the quality of our customers, but also have to make them flew together, serving them, who told them is our clothes, food, food. So, if you meet what you need, what you need, that is not our quality, and no matter what we practice, you can't produce a point of benefits to this, in such a customer, we should thank God. Thank God, Allah, thank you Buddha compassion, thank you all those who can thank, but don't "thank you" yourself. "Program" is the guy we eat, the more you play it, the more you want it. So, if I really encounter the kind of customer mentioned above, I spend time thanks to thank you, it is better to take my own procedures, so that the customers like it (also say that Saint Ming God universal Allah see you "piety", will let you meet a few such customers)!
See more clearly
Where is the type of release of the event, this point is on the surface, it should be in DMMSG, but in fact the CLR hides some things - a private field, the field is an instance of a delegate type, you can see it Become a linked list to store relevant types of subscription information, DMMSG is to store relevant subscription information using this field. The CLR allows the programmer to display the definition of this field, although more code, but the effect (lifting performance) is worth doing.
// define delegate public delegate void DecisionMakingEventHandler (System.Object source, DecisionMakingEventArgs mEventArgs); // explicitly defined list private DecisionMakingEventHandler dmDelegate; // define the event, but the event registration and event cancellation will need to display the definition of public event DecisionMakingEventHandler DmMsg {// event registration add {dmDelegate = (DecisionMakingEventHandler) System.Delegate.Combine (dmDelegate, value);} // event cancellation remove {dmDelegate = (DecisionMakingEventHandler) System.Delegate.Remove (dmDelegate, value);}} make local changes also There is an OnDMMSG method, and the alarm is changed from the list field "DMDelegate (this, e)". In this way, the new version of the event is completed.
real
This article will not be high to go, but after reading, there should be someone else.
Reference book
l Jeffrey Richter with "Applied Microsoft .NET Framework Programming", Microsoft Press Publishing
More than 2 autumn rain, "Millennium, sigh", writer publishing society