private void DynamicEventHandler() { dynamic h = new DynamicEventTestHandler(); int expected = rand.Next(); // Add handler obj.OnEvent += h; // Fire event obj.FireEvent(expected); h.Handler.Validate(true, expected); // Remove handler obj.OnEvent -= h; h.Handler.Reset(); // Fire event expected = rand.Next(); obj.FireEvent(expected); h.Handler.Validate(false); // Re-add handler obj.OnEvent += h; h.Handler.Reset(); // Fire event expected = rand.Next(); obj.FireEvent(expected); h.Handler.Validate(true, expected); obj.OnEvent -= h; }
private void MultipleHandlers() { var h1 = new EventTestHandler(); var h2 = new EventTestHandler(); dynamic dh1 = new DynamicEventTestHandler(); dynamic dh2 = new DynamicEventTestHandler(); int expected = rand.Next(); // Add handlers obj.OnEvent += h1.Handler; obj.OnEvent += h2.Handler; obj.OnEvent += dh1; obj.OnEvent += dh2; // Fire event obj.FireEvent(expected); h1.Validate(true, expected); h2.Validate(true, expected); dh1.Handler.Validate(true, expected); dh2.Handler.Validate(true, expected); // Remove first handler obj.OnEvent -= h1.Handler; obj.OnEvent -= dh1; h1.Reset(); h2.Reset(); dh1.Handler.Reset(); dh2.Handler.Reset(); // Fire event expected = rand.Next(); obj.FireEvent(expected); h1.Validate(false); h2.Validate(true, expected); dh1.Handler.Validate(false); dh2.Handler.Validate(true, expected); // Remove second handler obj.OnEvent -= h2.Handler; obj.OnEvent -= dh2; h1.Reset(); h2.Reset(); dh1.Handler.Reset(); dh2.Handler.Reset(); // Fire event expected = rand.Next(); obj.FireEvent(expected); h1.Validate(false); h2.Validate(false); dh1.Handler.Validate(false); dh2.Handler.Validate(false); }