Example #1
0
        public void Syntax_StaticEvent([Column(false, true)] bool usingObjectMirror)
        {
            var mirror = usingObjectMirror ? Mirror.ForObject(new SampleObject()) : Mirror.ForType <SampleObject>();

            SampleObject.staticCustomEvent = null;
            SampleObject.staticCustomEventAddRemoveCount = 0;

            Assert.AreEqual("StaticCustomEvent", mirror["StaticCustomEvent"].MemberInfo.Name);

            // adding/removing null should work event though no handlers will be registered
            mirror["StaticCustomEvent"].AddHandler((Delegate)null);
            mirror["StaticCustomEvent"].AddHandler((EventHandler)null);
            mirror["StaticCustomEvent"].AddHandler((EventHandler <EventArgs>)null);
            mirror["StaticCustomEvent"].RemoveHandler((Delegate)null);
            mirror["StaticCustomEvent"].RemoveHandler((EventHandler)null);
            mirror["StaticCustomEvent"].RemoveHandler((EventHandler <EventArgs>)null);
            Assert.AreEqual(6, SampleObject.staticCustomEventAddRemoveCount);
            Assert.IsNull(SampleObject.staticCustomEvent);

            // adding other handlers
            int handled = 0;

            SampleObject.CustomEventHandler nonCoercedHandler = (sender, e) => { handled++; };
            mirror["StaticCustomEvent"].AddHandler(nonCoercedHandler);
            mirror["StaticCustomEvent"].AddHandler((sender, e) => { handled++; });             // coerced
            mirror["StaticCustomEvent"].AddHandler <EventArgs>((sender, e) => { handled++; }); // coerced
            Assert.AreEqual(9, SampleObject.staticCustomEventAddRemoveCount);
            Assert.IsNotNull(SampleObject.staticCustomEvent);
            SampleObject.RaiseStaticCustomEvent();
            Assert.AreEqual(3, handled);

            // remove handler (non-coerced only)
            mirror["StaticCustomEvent"].RemoveHandler(nonCoercedHandler);
            Assert.AreEqual(10, SampleObject.staticCustomEventAddRemoveCount);
            Assert.IsNotNull(SampleObject.staticCustomEvent);
            SampleObject.RaiseStaticCustomEvent();
            Assert.AreEqual(5, handled);
        }