static void Main(string[] args) { Console.ReadLine(); Customer customer = new Customer(); // 事件拥有者 Watier watier = new Watier(); // 事件响应者 customer.Order += watier.Action; // 事件本身以及事件的订阅关系以及事件处理器 //customer.Action();// customer内部触发事件 OrderEventArgs e = new OrderEventArgs(); e.DishName = "Manhanquanxi"; e.Size = "large"; OrderEventArgs e2 = new OrderEventArgs(); e2.DishName = "Beer"; e2.Size = "large"; // badGuy借刀杀人 只要将Order声明为事件(即添加event关键字)即可避免 //Customer badGuy = new Customer(); //badGuy.Order += watier.Action; //badGuy.Order.Invoke(customer,e); //badGuy.Order.Invoke(customer,e2); customer.PayTheBill(); }
static void Main(string[] args) { Customer customer = new Customer(); Watier watier = new Watier(); customer.Order += watier.Action; customer.Action();// customer内部触发事件 customer.PayTheBill(); }