Inheritance: IDelegateReference
示例#1
0
        public void Not_Keep_Alive_Should_Allow_Delegate_To_Be_Collected()
        {
            var delegates = new SomeClassHandler();
            var delegateReference = new DelegateReference((Action<string>) delegates.DoEvent, false);

            // ReSharper disable RedundantAssignment
            delegates = null;
            // ReSharper restore RedundantAssignment
            GC.Collect();

            Assert.Null(delegateReference.Target);
        }
示例#2
0
        public void Keep_Alive_Should_Prevent_Delegate_From_Being_Collected()
        {
            var delegates = new SomeClassHandler();
            var delegateReference = new DelegateReference((Action<string>)delegates.DoEvent, true);

            // ReSharper disable RedundantAssignment
            delegates = null;
            // ReSharper restore RedundantAssignment
            GC.Collect();

            Assert.NotNull(delegateReference.Target);
        }
示例#3
0
        public void Should_Allow_Collection_Of_Original_Delegate()
        {
            var classHandler = new SomeClassHandler();
            Action<string> myAction = classHandler.MyAction;

            var weakAction = new DelegateReference(myAction, false);

            var originalAction = new WeakReference(myAction);
            // ReSharper disable RedundantAssignment
            myAction = null;
            // ReSharper restore RedundantAssignment
            GC.Collect();
            Assert.False(originalAction.IsAlive);

            ((Action<string>)weakAction.Target)("payload");
            Assert.Equal("payload", classHandler.MyActionArg);
        }
示例#4
0
        public void Should_Return_Null_If_Target_NotAlive()
        {
            SomeClassHandler handler = new SomeClassHandler();
            var weakHandlerRef = new WeakReference(handler);

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

            // ReSharper disable RedundantAssignment
            handler = null;
            // ReSharper restore RedundantAssignment
            GC.Collect();
            Assert.False(weakHandlerRef.IsAlive);
            Assert.Null(action.Target);
        }
示例#5
0
        public void WeakDelegate_Should_Works_With_Static_Method_Delegates()
        {
            var action = new DelegateReference((Action)SomeClassHandler.StaticMethod, false);

            Assert.NotNull(action.Target);
        }
示例#6
0
        public void Target_Should_Return_Action()
        {
            var classHandler = new SomeClassHandler();
            Action<string> myAction = classHandler.MyAction;

            var weakAction = new DelegateReference(myAction, false);

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

            Assert.Equal("payload", classHandler.MyActionArg);
        }