Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void Syntax_InstanceEvent()
        {
            var obj    = new SampleObject();
            var mirror = Mirror.ForObject(obj);

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

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

            // adding other handlers
            int handled = 0;

            SampleObject.CustomEventHandler nonCoercedHandler = (sender, e) => { handled++; };
            mirror["InstanceCustomEvent"].AddHandler(nonCoercedHandler);
            mirror["InstanceCustomEvent"].AddHandler((sender, e) => { handled++; });             // coerced
            mirror["InstanceCustomEvent"].AddHandler <EventArgs>((sender, e) => { handled++; }); // coerced
            Assert.AreEqual(9, obj.instanceCustomEventAddRemoveCount);
            Assert.IsNotNull(obj.instanceCustomEvent);
            obj.RaiseInstanceCustomEvent();
            Assert.AreEqual(3, handled);

            // remove handler (non-coerced only)
            mirror["InstanceCustomEvent"].RemoveHandler(nonCoercedHandler);
            Assert.AreEqual(10, obj.instanceCustomEventAddRemoveCount);
            Assert.IsNotNull(obj.instanceCustomEvent);
            obj.RaiseInstanceCustomEvent();
            Assert.AreEqual(5, handled);
        }