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}