示例#1
0
        /// <summary>
        ///    Method invoked when (instead of) a new handler is added to the target event.
        /// </summary>
        /// <param name="args">Context information.</param>
        public override void OnAddHandler(EventInterceptionArgs args)
        {
            var  weakEventClient       = args.Handler.Target as IWeakEventClient;
            bool supportsWeakReference = weakEventClient != null;

            // Throw an exception if the client does not support weak events and we are not allowed to hold strong references.
            if (!supportsWeakReference && args.Handler.Target != null && !AllowStrongReferences)
            {
                throw new InvalidOperationException(
                          $"Attempt to add a reference to the weak event {args.Event} from type {args.Handler.Target.GetType()}, which does not implement the IWeakEventClient interface.");
            }

            // Add the handler to our own list.
            if (_weakEventHandler.AddHandler(args.Handler, supportsWeakReference))
            {
                // If it is the first handler we are adding, add a fake handler to ourselves to the target event.
                // Whichever handler will add here will be passed to OnInvokeHandler, so it is safe and convenient to pass null.
                args.AddHandler(null);
            }

            // Register the handler to the client to prevent garbage collection of the handler.
            if (supportsWeakReference)
            {
                weakEventClient.RegisterEventHandler(args.Handler);
            }
        }
        /// <summary>
        ///   Method invoked when (instead of) a new handler is added to the target event.
        /// </summary>
        /// <param name="args">Context information.</param>
        public override void OnAddHandler(EventInterceptionArgs args)
        {
            // Add the handler to our own list.
            if (AddHandler(args.Handler))
            {
                args.AddHandler(null);
            }


            // Register the handler to the client to prevent garbage collection of the handler.
            DelegateReferenceKeeper.AddReference(args.Handler);
        }
        /// <summary>
        ///     Method invoked when (instead of) a new handler is added to the target event.
        /// </summary>
        /// <param name="args">Context information.</param>
        public override void OnAddHandler(EventInterceptionArgs args)
        {
            // Add the handler to our own list.
            if (AddHandler(args.Handler))
            {
                // If it is the first handler we are adding, add a fake handler to ourselves to the target event.
                // Whichever handler will add here will be passed to OnInvokeHandler, so it is safe and convenient to pass null.
                args.AddHandler(null);
            }


            // Register the handler to the client to prevent garbage collection of the handler.
            DelegateReferenceKeeper.AddReference(args.Handler);
        }