In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Preface
Following the previous delegate, we continue to explore the event, because the delegate and the event are inextricably related. Through this article, I believe you will have a deeper understanding and understanding of the event, do not believe it, you see!
Concept
Use the event keyword to enable you to declare events. An event is a method by which a class notifies when something related occurs. [summary] an event is a way for a class to provide notification when something of its concern occurs.
Introduce
To understand events, you must first know the following two roles:
Publisher (Publisher)
The event publisher, also known as the sender, is simply an object that maintains its own state information. When their own status information changes, an event is triggered and subscribers of all events are notified.
Subscriber (Subscriber)
Objects interested in events, also known as recevier, can register the events you are interested in, usually providing an event handler that automatically triggers the content of the code when the event publisher triggers an event.
The most classic example is that a user subscribes to a magazine from a newspaper. Here is a picture to illustrate the flow of the event.
The newspaper has a variety of magazines, when users want to view the magazine, they must first subscribe to the magazine, when the newspaper publishes the magazine, users who subscribe to the magazine can receive the magazine! So in this example, the event is the magazine event published by the newspaper, and the magazine is distributed to users who have already subscribed to the magazine, thus forming a complete event mechanism. Let's use code to implement this example.
1 public class Publisher / * publisher * / 2 {3 public delegate void Publish (); / * event Agent * / 4 public event Publish OnPublish; / * event * / 5 public void Issue () / * trigger event method * / 6 {7 if (OnPublish! = null) 8 {9 Console.WriteLine ("publication") 10 OnPublish (); 11} 12} 13} 14 public class Subscriber / * subscriber * / 15 {16 public void Recieve () / * subscriber event handler * / 17 {18 Console.WriteLine ("accept Publications") 19} 20} 21 class Program22 {23 static void Main (string [] args) 24 {25 Publisher pub = new Publisher (); 26 Subscriber user = new Subscriber (); 27 pub.OnPublish + = new Publisher.Publish (user.Recieve); / * subscribe to an event publisher * / 28 pub.Issue (); 29 Console.ReadKey () 30} 31}
Public delegate void Publish (); proxy for the event. The method by which Issue () triggers the event. Recieve () subscriber event handler. I / O of the console:
Through this example, we must have a preliminary understanding of the event mechanism. Next, in order to have a more realistic understanding of the event mechanism, let's assume the following scenario: because the newspaper is doing well, it has achieved good results in a short period of time, and began to publish two kinds of magazines, one is blog Garden Magazine, the other is blog Magazine. In view of this, the code will be rewritten as follows:
1 public class Publisher 2 {3 public delegate void PubCnblogs (string magezineName); 4 public delegate void PubCnblogsQuestion (string magezineName); 5 public event PubCnblogs OnPubCnblogs; 6 public event PubCnblogsQuestion OnPubCnblogsQuestion; 7 public void IssueCnblogs () 8 {9 if (OnPubCnblogs! = null) 10 {11 Console.WriteLine ("newspaper publishes blog Garden Magazine") 12 OnPubCnblogs ("blog Park"); 13} 14} 15 public void IssueCnblogsQuestion () 16 {17 if (OnPubCnblogsQuestion! = null) 18 {19 Console.WriteLine ("newspaper publishes blog magazines"); 20 OnPubCnblogsQuestion ("blog articles") 21} 22} 23} 24 public class Subscriber25 {26 public string Name {get; set;} 27 28 public Subscriber (string name) 29 {30 this.Name = name;31} 32 public void Recieve (string magezineName) 33 {34 Console.WriteLine (this.Name + accept + magezineName + Magazine) 35} 36} 37 class Program38 {39 static void Main (string [] args) 40 {41 Publisher pub = new Publisher (); 42 Subscriber xh = new Subscriber ("Xiao Hong"); 43 pub.OnPubCnblogs + = new Publisher.PubCnblogs (xh.Recieve); 44 45 46 Subscriber xm = new Subscriber ("Xiao Ming") 47 pub.OnPubCnblogs + = new Publisher.PubCnblogs (xm.Recieve); 48 pub.OnPubCnblogsQuestion + = new Publisher.PubCnblogsQuestion (xm.Recieve); 49 50 pub.IssueCnblogs (); 51 pub.IssueCnblogsQuestion (); 52 53 Console.WriteLine (); 54 Console.WriteLine ("after a while"); 55 pub.OnPubCnblogsQuestion-= new Publisher.PubCnblogsQuestion (xm.Recieve) 56 pub.IssueCnblogs (); 57 pub.IssueCnblogsQuestion (); 58 Console.ReadKey (); 59} 60 61 62}
Look at the above code newspaper issued blog Garden Magazine and blog ask Magazine, Xiao Hong only subscribed to blog Garden Magazine, Xiao Ming subscribed to blog Garden Magazine and blog ask Magazine, and triggered the release of these two magazines through IssueCnblogs and IssueCnblogsQuestion methods. After a period of time, Xiao Ming cancelled the subscription to blog Park Magazine, and in the end, Xiao Hong and Xiao Ming received subscribed blog Garden magazines. Print as follows:
To sum up, we conclude that there are four steps to define a complete event:
Defining events in event publishers event triggering events in event publishers naming guidelines for event design guidelines events that are scheduled for event handlers event subscribers to subscribe to event publishers should use the naming method of PascalCasing. When declaring delegate, the void type is used as the return value, the event delegate for the EventName event is EventNameHandler, and the event accepts two parameters, both named sender and e. Define a class that provides event data, name the class as EventNameEventArgs, derive it from System.EventArgs, and then add specific members of all events. If the event does not need to pass any parameters, also declare two parameters, the e parameter can directly use the system-provided System.EventArgs class, if you need to pass data, inherit a class from System.EventArgs and put the data in it. For example, public delegate void EventNameHandler (object sender, EventArgs e) public event EventNameHandler EventName; provides a protected method in the class that triggers the event. Named after EventName, the event is triggered in this method. For example:
Protected virtual void OnEventName (EventArgs e) {if (EventName! = null) {EventName (this,e);}}
The following norms transform the distribution of magazines by newspapers and subscribers to magazines, as follows:
1 public class PubEventArgs: EventArgs / * Class PubEventArgs*/ 2 {3 private readonly string magezine_name; 4 private readonly DateTime magezine_time; 5 public PubEventArgs (string magezineName, DateTime magezineTime) 6 {7 magezine_name = magezineName; 8 magezine_time = magezineTime; 9} 10 public string magezineName11 {12 get {return magezine_name } 13} 14 15 public DateTime magezineTime16 {17 get {return magezine_time;} 18} 19} 20 public class Publisher / * publisher (newspaper that is the publisher of the event) * / 21 {22 public delegate void PubCnblogsEventHandler (object sender, PubEventArgs e); / * entrusted event agent for publishing blog Park magazine * / 23 public delegate void PubCnblogsQuestionEventHandler (object sender, PubEventArgs e) / * entrusted event agent for publishing blog park magazine * / 24 public event PubCnblogsEventHandler PubCnblogs; / * release of blog park magazine event * / 25 public event PubCnblogsQuestionEventHandler PubCnblogsQuestion; / * release of blog park magazine event * / 26 27 public virtual void OnPubCnblogs (PubEventArgs e) / * provides a protected method for triggering the publication of blog park magazine event * / 28 {29 PubCnblogsEventHandler cnblogs = PubCnblogs 30 if (cnblogs! = null) 31 {32 cnblogs (this, e); 33} 34} 35 public virtual void OnPubCnblogsQuestion (PubEventArgs e) / * provides a protected method for triggering a blog magazine event * / 36 {37 PubCnblogsQuestionEventHandler question = PubCnblogsQuestion 38 if (question! = null) 39 {40 question (this, e); 41} 42} 43 public void IssueCnblogs (string magezine_name, DateTime magezine_time) / * method to trigger the publication of blog Park magazines * / 44 {45 Console.WriteLine ("publish" + magezine_name) 46 OnPubCnblogs (new PubEventArgs (magezine_name, magezine_time)); 47} 48 public void IssueCnblogsQuestion (string magezine_name, DateTime magezine_time) / * method to trigger the publication of blog magazines * / 49 {50 Console.WriteLine ("release" + magezine_name); 51 OnPubCnblogsQuestion (new PubEventArgs (magezine_name, magezine_time)) 52} 53} 54 public class Subscriber / * subscriber (user is the recipient of the event) * / 55 {56 public string Name {get; set;} 57 58 public Subscriber (string name) 59 {60 this.Name = name 61} 62 public void Recieve (object sender, PubEventArgs e) / * subscriber subscription event handler * / 63 {64 Console.WriteLine (e.magezineTime + "+ Name +" received "+ e.magezineName); 65} 66} 67 class Program68 {69 static void Main (string [] args) 70 {71 Publisher pub = new Publisher () / * instantiated publisher * / 72 Subscriber xh = new Subscriber ("Xiao Hong"); / * instantiated subscriber Xiao Hong * / 73 pub.PubCnblogs + = new Publisher.PubCnblogsEventHandler (xh.Recieve); / * Xiao Hong subscribed to blog Park Magazine * / 74 75 76 Subscriber xm = new Subscriber ("Xiao Ming") / * instantiated subscriber Xiaoming * / 77 pub.PubCnblogs + = new Publisher.PubCnblogsEventHandler (xm.Recieve); / * Xiaoming subscribing to blog Park Magazine * / 78 pub.PubCnblogsQuestion + = new Publisher.PubCnblogsQuestionEventHandler (xm.Recieve); / * Xiaoming subscribing to blog Park Magazine * / 79 80 pub.IssueCnblogs (blog Park Magazine, DateTime.Now) / * newspaper publishes blog Garden Magazine (triggered blog Garden Magazine incident) * / 81 pub.IssueCnblogsQuestion (blog ask Magazine, DateTime.Now); / * newspaper issues blog question Magazine (trigger blog question Magazine incident) * / 82 83 Console.WriteLine (); 84 85 Console.WriteLine ("after a while"); 86 87 pub.PubCnblogsQuestion + = new Publisher.PubCnblogsQuestionEventHandler (xh.Recieve) / * Xiao Hong thought blog Park magazine was good and continued to subscribe to blog Park Magazine * / 88 pub.PubCnblogsQuestion-= new Publisher.PubCnblogsQuestionEventHandler (xm.Recieve); / * Xiao Ming thought it was a little too much to read, so he canceled his subscription to blog Park Magazine * / 89 pub.IssueCnblogs (blog Park Magazine, DateTime.Now) / * newspaper publishes blog Park Magazine (triggers blog Garden Magazine incident) * / 90 pub.IssueCnblogsQuestion (blog ask Magazine, DateTime.Now); / * newspaper issues blog question Magazine (trigger blog question Magazine incident) * / 91 Console.ReadKey (); 92} 93 94 95}
Print out according to the above code:
Through the above study, we know the event and the basic usage as well as the related naming convention, let's explore the nature of the event.
Event essence
Through the Publisher publisher class we wrote above, we look at its IL code through the decompiler tool:
Let's just look at the blog delegate in the publisher. The delegate variable we defined in the code above is public event PubCnblogsQuestionEventHandler PubCnblogsQuestion. But at this point, we find that the access to the variable becomes private private, and two methods add and remove are added to the private delegate variable, so we come to the following two conclusions: (1) automatically make the delegate variable private (2) generate both add () and remove () methods by adding the event keyword. This means that + = and-= in the event are implemented through the add () and remove () methods.
So let's take a look at what the add () method is.
The internal implementation of add () is finally implemented by calling the Combine method in the Delegate of the final parent class of the delegate, and we can also see the this.PubCnblogsQuestion. From the above we know that the delegate variable declared with the event keyword is private, so where does the PubCnblogsQuestion variable of this call come from? It is not difficult to see that the delegate variable with the same name is automatically generated in the declaration of the event keyword. If you don't believe it, please take a look at the operation in the picture below, which will make you believe it.
When the publisher class is instantiated and then its delegate variable PubCnblogsQuestion is assigned and then compiled and generated will not pass, it is also said above that what you are operating is that the event keyword will automatically generate a variable with the same name as the declared delegate variable can only operate on it + = or-=, while the delegate variable you declare is private and you cannot access it. This point of view is also verified by this.
By the same token, we look at the implementation of its remove () method, and as you can guess, it must also be the Remove () method in the final parent class Delegate in the operation delegate to operate on it. Seeing is believing:
To sum up the essence of the event, that is, when the essence of the event keyword is used to modify the delegate variable with the event keyword, the delegate variable will be privatized, and at the same time, a variable with the same name as the delegate variable will be automatically generated when the delegate variable is modified with the event keyword, the two methods add () and remove () will be automatically generated, that is, the + = or-= in the event is realized through these two methods. At the same time, the final implementation is to manipulate the functions of the Combine () and Reomve () methods in the final parent class Delegate of the privatized delegate object (benefits)
How can we see the event of delegated dependency? So think about what would happen if there were no events, and what if the last programmer used the delegate in the actual project, when the programmer packed up and left, and when the next programmer came, he didn't know what was implemented or remove the delegate implementation he wrote! If there is an event, you can't manipulate the delegate object at all, you can only do it through + = and-=, thus avoiding the abuse of delegate!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.