예제 #1
0
        /// <summary>
        /// 创建与指定委托列表相关的方法切换器,会自动推断关键参数(使用不同子类的参数,必须是唯一的)。
        /// </summary>
        /// <typeparam name="TDelegate">使用基类型调用方法的委托类型。</typeparam>
        /// <param name="delegates">使用不同子类作为参数的委托列表。</param>
        /// <returns>与 <paramref name="delegates"/> 相关的方法切换器。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="delegates"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><paramref name="delegates"/> 中存在为 <c>null</c> 的委托。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">委托类型与处理器不匹配。</exception>
        /// <exception cref="ArgumentException">处理器的参数不匹配。</exception>
        /// <exception cref="ArgumentException">没有找到唯一的关键参数。</exception>
        /// <overloads>
        /// <summary>
        /// 创建方法切换器,会自动推断关键参数(使用不同子类的参数,必须是唯一的)。
        /// </summary>
        /// </overloads>
        public static TDelegate Create <TDelegate>(params Delegate[] delegates)
            where TDelegate : class
        {
            if (delegates == null)
            {
                throw CommonExceptions.ArgumentNull("delegates");
            }
            if (delegates.Any(d => d == null))
            {
                throw CommonExceptions.CollectionItemNull("delegates");
            }
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            if (!typeof(TDelegate).IsSubclassOf(typeof(Delegate)))
            {
                throw CommonExceptions.MustBeDelegate("TDelegate", typeof(TDelegate));
            }
            ProcessorData data = new ProcessorData(delegates);

            CheckDelegateType <TDelegate>(data);
            return(CreateSwitcher <TDelegate>(data, null, null));
        }