コード例 #1
0
        /// <summary>
        /// This method provides an implementation for an <see cref="IComposer{TTarget}"/>
        /// to handle this event. This checks the <see cref="Participant"/>, and the
        /// <see cref="Request"/>, and will invoke your <paramref name="composer"/>
        /// as specified.
        /// </summary>
        /// <typeparam name="T">Your <paramref name="composer"/>
        /// actual (covariant) type.</typeparam>
        /// <param name="composer">Not null.</param>
        /// <returns>True if any method was invoked on the <paramref name="composer"/>.
        /// False if not invoked.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public bool Handle <T>(IComposer <T> composer)
            where T : TTarget
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            if (Participant == null)
            {
                return(false);
            }
            IComposer <TTarget> targetComposer = composer as IComposer <TTarget>;

            switch (Request)
            {
            case ParticipantRequest.Add:
                if (targetComposer != null)
                {
                    targetComposer.Participate(Participant);
                    return(true);
                }
                composer.ParticipateAs(Participant);
                return(true);

            case ParticipantRequest.AddOneTimeOnly:
                if (targetComposer != null)
                {
                    targetComposer.Participate(Participant, true);
                    return(true);
                }
                composer.ParticipateAs(Participant, true);
                return(true);

            case ParticipantRequest.Remove:
                if (targetComposer != null)
                {
                    targetComposer.Remove(Participant);
                    return(true);
                }
                composer.Remove(new VariantParticipant <TTarget, T>(Participant));
                return(true);
            }
            return(false);
        }