using System; class Class1 { static void Main() { Student s1=new Student(); s1.Name ="Student1"; Student s2=new Student(); s2.Name ="Student2"; s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK); s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK); //当Register方法一执行,触发RegisterOK事件 //RegisterOK事件一触发,然后执行Student_RegisterOK方法 s1.Register(); s2.Register(); Console.ReadLine(); } static void Student_RegisterOK(RegisterOkArgs e) { Console.WriteLine(e.EventInfo); } } class Student { public delegate void DelegateRegisterOkEvent(RegisterOkArgs e); public event DelegateRegisterOkEvent RegisterOK; public string Name; public void Register() { Console.WriteLine("Register Method"); RegisterOK(new RegisterOkArgs("Student Name: "+Name)); } } class RegisterOkArgs:EventArgs { public string EventInfo; public RegisterOkArgs(string eventInfo):base() { this.EventInfo =eventInfo; } } |