Recently, the project needs to use the. NET Framework, C #. I have never been in touch with it before. I barely learned it. It feels okay and it is not difficult to learn it.
Recently, the school "Events", the classic pub-Sub Model, in. net, there is a method called delegate, the keyword delegate, this method is a bit similar to the interface, but more flexible than the excuse, because delegate supports broadcast effect, heavy load a lot of Operation parent, such as + =, +,-=, and so on.
In pub, declare a delegate handler and define the signature (that is, parameter settings). In sub, we pass pub as a parameter to sub, in addition, sub automatically registers its own method with the pub handler (which can be understood as the callback function in c), so that when pub needs to broadcast, it can directly call the handler, instead of paying attention to who registered handler.
In C #, eventhandler is a delegate. Create an eventhandler attribute in pub. assign a value to this attribute in sub.
The Code is as follows:
namespace DotNetEvents{ using System; using System.Collections.Generic; // Define a class to hold custom event info public class CustomEventArgs : EventArgs { public CustomEventArgs(string s) { message = s; } private string message; public string Message { get { return message; } set { message = value; } } } // Class that publishes an event class Publisher { // Declare the event using EventHandler<T> public event EventHandler<CustomEventArgs> RaiseCustomEvent; public void DoSomething() { // Write some code that does something useful here // then raise the event. You can also raise an event // before you execute a block of code. OnRaiseCustomEvent(new CustomEventArgs("Did something")); } // Wrap event invocations inside a protected virtual method // to allow derived classes to override the event invocation behavior protected virtual void OnRaiseCustomEvent(CustomEventArgs e) { // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. EventHandler<CustomEventArgs> handler = RaiseCustomEvent; // Event will be null if there are no subscribers if (handler != null) { // Format the string to send inside the CustomEventArgs parameter e.Message += String.Format(" at {0}", DateTime.Now.ToString()); // Use the () operator to raise the event. handler(this, e); } } } //Class that subscribes to an event class Subscriber { private string id; public Subscriber(string ID, Publisher pub) { id = ID; // Subscribe to the event using C# 2.0 syntax pub.RaiseCustomEvent += HandleCustomEvent; } // Define what actions to take when the event is raised. void HandleCustomEvent(object sender, CustomEventArgs e) { Console.WriteLine(id + " received this message: {0}", e.Message); } } class Program { static void Main(string[] args) { Publisher pub = new Publisher(); Subscriber sub1 = new Subscriber("sub1", pub); Subscriber sub2 = new Subscriber("sub2", pub); // Call the method that raises the event. pub.DoSomething(); // Keep the console window open Console.WriteLine("Press Enter to close this window."); Console.ReadLine(); } }}