How to: publish events that comply with the. NET Framework Guidelines (C # programming guide)

Source: Internet
Author: User

C # programming guide

How to: publish events that comply with the. NET Framework Guidelines (C # programming guide)

The following process demonstrates how to add events that conform to the standard. NET Framework pattern to your own classes and structures .. All events in the Net Framework class library are based on the eventhandler delegate and are defined as follows: copy the code public delegate void eventhandler (Object sender, eventargs E );

Note:
. NET Framework 2.0 introduces a generic version of this delegate, namely, eventhandler. The following example shows how to use these two versions. <T>
Although the events in the class you define can adopt any valid delegate type (including the delegate that will return values), we recommend that you use EventhandlerLet the event adopt. net Framework mode, as shown in the following example: Use eventhandler mode to publish events (skip this step if you do not need to send custom data containing events, and directly go to step 3A .) Declare the class in the visible range of the publisher class and the subscription class, and add the members required to retain the Custom Event data. In this example, a simple string is returned. Copy the public class customeventargs: eventargs {public customeventargs (string s) {MSG = s;} private string MSG; Public String message {get {return MSG ;}}} (If you are using EventhandlerSkip this step .) Declare a delegate in the release class. Specify the name ending with eventhandler for it. The second parameter specifies the custom eventargs type. Copy the code public delegate void customeventhandler (Object sender, customeventargs A). Use Any of the following steps to declare an event in the release class. If the eventargs class is not customized, the event type is a non-generic eventhandler delegate. It does not need to be declared because it has been declared in the default system namespace contained in the C # project: copy the code public event eventhandler raisecustomevent; if you are using EventhandlerAnd you have a custom class derived from eventargs. Declare your event in the release class and use your delegate as the type: copy the code class publisher {public event customeventhandler raisecustomevent;} if you are using a generic version, no custom delegation is required. Instead, specify the event type Eventhandler <customeventargs>And place the name of your class in the angle brackets. Copy the public event eventhandler <customeventargs> raisecustomevent code. The following example demonstrates the preceding steps. Eventhandler <t>Used as the event type. C # copy the code namespace dotnetevents {using system; using system. collections. generic; // define a class to hold Custom Event info publicclass customeventargs: eventargs {public customeventargs (string s) {message = s;} privatestring message; publicstring message {get {return message;} set {message = value ;}}// class that publishes an event class publisher {// declare the event using eventhandl Er <t> public event eventhandler <customeventargs> raisecustomevent; publicvoid 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 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 {privatestring 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 {staticvoid 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 () ;}}( Source: msdn)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.