Ejemplo n.º 1
0
        public void NotKeepAliveShouldAllowDelegateToBeCollected()
        {
            var delegates = new TestClassHandler();
            var delegateReference = new DelegateReference((Action<string>) delegates.DoEvent, false);

            delegates = null;

            GC.Collect();

            Assert.That(delegateReference.Target, Is.Null);
        }
Ejemplo n.º 2
0
        public void KeepAliveShouldPreventDelegateFromBeingCollected()
        {
            var delegates = new TestClassHandler();
            var delegateReference = new DelegateReference((Action<string>)delegates.DoEvent, true);

            delegates = null;

            GC.Collect();

            Assert.That(delegateReference.Target, Is.Not.Null);
        }
Ejemplo n.º 3
0
        public void ShouldAllowCollectionOfOriginalDelegate()
        {
            var classHandler = new TestClassHandler();
            Action<string> myAction = classHandler.MyAction;

            var weakAction = new DelegateReference(myAction, false);

            var originalAction = new WeakReference(myAction);

            myAction = null;

            GC.Collect();
            Assert.That(originalAction.IsAlive, Is.False);

            ((Action<string>)weakAction.Target)("payload");
            Assert.That(classHandler.MyActionArg, Is.EqualTo("payload"));
        }
Ejemplo n.º 4
0
        public void TargetShouldReturnAction()
        {
            var classHandler = new TestClassHandler();
            Action<string> myAction = classHandler.MyAction;

            var weakAction = new DelegateReference(myAction, false);

            ((Action<string>) weakAction.Target)("payload");

            Assert.That(classHandler.MyActionArg, Is.EqualTo("payload"));
        }
Ejemplo n.º 5
0
        public void ShouldReturnNullIfTargetNotAlive()
        {
            var handler = new TestClassHandler();
            var weakHandlerRef = new WeakReference(handler);

            var action = new DelegateReference((Action<string>)handler.DoEvent, false);

            handler = null;

            GC.Collect();
            Assert.That(weakHandlerRef.IsAlive, Is.False);
            Assert.That(action.Target, Is.Null);
        }