Emit learning-Step by step-defining events

Source: Internet
Author: User
Tags emit rand

Before studying how to add an event to a dynamic class with emit, originally thought would be very simple, but encountered a lot of problems, some of the questions in the previous answer article has been mentioned, and have been resolved, although some places themselves are not very clear, but after all, is resolved, and finally compare my write IL code, and system Automatically generated, there are always some places can not do the same. In particular, when adding the Add and remove methods to an event, there are a number of problems that I will explain below. According to the Convention, first give the C # code of the class to be implemented, easy to decompile and follow the code of IL Code as follows:

Publisher

 public class Publisher {private bool Isstart = false;


    Private Random Random = new Random (DateTime.Now.Millisecond);

            public void Start () {if (!isstart) {Isstart = true;

        Generaterand ();

    The public void Stop () {Isstart = false; private void Generaterand () {while (Isstart) {onrandgenerated (random).

            Next (10000));

        Thread.Sleep (1000);


    } #region Event public event eventhandler<randgeneratedeventargs> randgenerated;

    protected virtual void onrandgenerated (int rand) {raiserandgeneratedevent (rand); 

private void raiserandgeneratedevent (int rand) {Eventhandler<randgeneratedeventargs> temp =

        randgenerated;

            if (temp!= null) {Randgeneratedeventargs arg = new Randgeneratedeventargs (rand); Temp (this, ARG); } #endregion} 

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.