Ejemplo n.º 1
0
        public void EventAddRemoveAnonymousFunction()
        {
            var x = new EventThing();

            x.MyEvent += (object sender, EventArgs args) => { HandlerToRemove(sender, args); };
            x.MyEvent -= (object sender, EventArgs args) => { HandlerToRemove(sender, args); };

            x.OnMyEvent();
        }
Ejemplo n.º 2
0
        public void EventAddRemoveMethod()
        {
            var x = new EventThing();

            x.MyEvent += HandlerToRemove;
            x.MyEvent -= HandlerToRemove;

            x.OnMyEvent();
        }
Ejemplo n.º 3
0
        public void EventAddRemoveLambda()
        {
            var x = new EventThing();

            x.MyEvent += (sender, args) => HandlerToRemove(sender, args);
            x.MyEvent -= (sender, args) => HandlerToRemove(sender, args);

            x.OnMyEvent();
        }
Ejemplo n.º 4
0
        public void EventAddRemoveMethodRef()
        {
            var x = new EventThing();

            EventHandler handlerToRemove = HandlerToRemove;

            x.MyEvent += handlerToRemove;
            x.MyEvent -= handlerToRemove;

            x.OnMyEvent();
        }
Ejemplo n.º 5
0
        public void EventAddRemoveLocalFunction()
        {
            var x = new EventThing();

            void handler(object sender, EventArgs args)
            {
                HandlerToRemove(sender, args);
            }

            x.MyEvent += handler;
            x.MyEvent -= handler;

            x.OnMyEvent();
        }
Ejemplo n.º 6
0
        public void TestEventNotNull()
        {
            EventThing thing = new EventThing();

            DoStuff(thing);

            Assert.AreEqual(EventSubscriber.Count, 1);

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.AreEqual(EventSubscriber.Count, 1, "Event sub list should still hold a subscriber reference");

            thing.OnMyEvent();
        }
Ejemplo n.º 7
0
        public void TestEventNull()
        {
            EventThing thing = new EventThing();

            DoStuff(thing);

            Assert.AreEqual(EventSubscriber.Count, 1);

            thing.SetEventNull();             // Should clear out the list, removing all refs to subscribers

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.AreEqual(EventSubscriber.Count, 0, "Setting event to null should have cleared sub list");

            thing.OnMyEvent();
        }
Ejemplo n.º 8
0
        public void NoObjectInitializersWithEvents()
        {
            bool firstHappened  = false;
            bool secondHappened = false;

            var x = new EventThing();

            // With Events, we required two statements to initialize
            x.MyEvent += (sender, args) => { firstHappened = true; };

            // And still do multiple handlers, like this:
            x.MyEvent += (sender, args) => { secondHappened = true; };

            x.OnMyEvent();

            Assert.True(firstHappened);
            Assert.True(secondHappened);
        }
Ejemplo n.º 9
0
 static void DoStuff(EventThing thing)
 {
     new EventSubscriber(thing);
     thing.OnMyEvent();
 }