public void SimulateMailArrived(string from, string to, string subject, string body) { MailEventArgs args = new MailEventArgs() { Body = body, From = from, Subject = subject, To = to }; if (MailArrived != null) { //MailArrived è null se nessuno si è sottoscritto, però poi nel foreach vado a tirarmi fuori la lista di Delegate esplicitamente //tramite le apposite funzioni foreach (Delegate item in MailArrived.GetInvocationList().ToList()) { //GetInvocationList() restituisce un'array di Delegates, che poi viene trasformato in una lista con il ToList() //Questo array contiene tutti i subscribers MailManagerEventHandler mm_eh = (MailManagerEventHandler)item; //riesco ad invocare il metodo attraverso il suo puntatore. Castando al Delegate specifico riesco a vedere esattamente che segnatura deve avere mm_eh. mm_eh(this, args); //il primo argomento è chi ha sollevato l'evento, che è proprio MailManager //qui invoco di volta in volta il metodo incapsulato dentro al delegate } // Avremmo potuto scrivere solo: MailArrived(this, args); } //O avremmo potuto scrivere tutto l'if come: MailArrived?.Invoke(this,args); }
public void SimulateMailArrived(string from, string to, string subject, string body) { MailEventArgs args = new MailEventArgs() { Body = body, From = from, Subject = subject, To = to }; #region altri modi di invocare la invocation list /* * // primo metodo * MailArrived?.Invoke(this, args); * * * // secondo metodo * if (MailArrived != null) * { * MailArrived(this, args); * } */ #endregion // metodo esplicito if (MailArrived != null) { // Rise Event!!!!!!!!!! foreach (var item in MailArrived.GetInvocationList()) { MailManagerEventHandler mm_eh = (MailManagerEventHandler)item; mm_eh(this, args); } } }
private void _mm_MailArrived(object sender, MailEventArgs args) { Console.WriteLine("I'm a Printer \r\n Mail From: {0} \t\t Mail to: {1} \r\n Subject: {2}\t\t Body: {3}", args.From, args.To, args.Subject, args.Body); }
public void mm_MailArrived(Object sender, MailEventArgs args) { Console.WriteLine("I'm a fax\r\n Mail from {0}\t\t Mail to {1} \r\n Subject: {2} \t\t Body: {3}", args.From, args.To, args.Subject, args.Body); }