Exemplo n.º 1
0
        /// <summary>
        /// 检查委托的类型是否合法。
        /// </summary>
        /// <param name="type">委托的类型。</param>
        /// <param name="paramName">参数的名称。</param>
        internal static void CheckDelegateType(Type type, string paramName)
        {
            CommonExceptions.CheckArgumentNull(type, paramName);
            Type baseType = type.BaseType;

            if (baseType != typeof(MulticastDelegate))
            {
                throw CommonExceptions.MustBeDelegate(paramName);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建指定对象中与默认指定相关的实例方法切换器,会自动推断关键参数(使用不同子类的参数,必须是唯一的)。
        /// </summary>
        /// <typeparam name="TDelegate">使用基类型调用方法的委托。</typeparam>
        /// <param name="target">在其中查找实例方法的对象。</param>
        /// <returns><paramref name="target"/> 中与默认标识相关的实例方法切换器。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="target"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">委托类型与处理器不匹配。</exception>
        /// <exception cref="ArgumentException">处理器的参数不匹配。</exception>
        /// <exception cref="ArgumentException">没有找到唯一的关键参数。</exception>
        public static TDelegate Create <TDelegate>(object target)
            where TDelegate : class
        {
            if (target == null)
            {
                throw CommonExceptions.ArgumentNull("target");
            }
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            if (!typeof(TDelegate).IsSubclassOf(typeof(Delegate)))
            {
                throw CommonExceptions.MustBeDelegate("TDelegate", typeof(TDelegate));
            }
            ProcessorData data = GetMethods <TDelegate>(target.GetType(), ProcessorAttribute.DefaultId, false);

            return(CreateSwitcher <TDelegate>(data, ProcessorAttribute.DefaultId, target));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建指定对象中与标识指定相关的实例方法切换器,会自动推断关键参数(使用不同子类的参数,必须是唯一的)。
        /// </summary>
        /// <typeparam name="TDelegate">使用基类型调用方法的委托。</typeparam>
        /// <param name="target">在其中查找实例方法的对象。</param>
        /// <param name="id">处理器的标识。</param>
        /// <returns><paramref name="target"/> 中与指定标识相关的实例方法切换器。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="target"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="id"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><paramref name="id"/> 为空字符串。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">委托类型与处理器不匹配。</exception>
        /// <exception cref="ArgumentException">处理器的参数不匹配。</exception>
        /// <exception cref="ArgumentException">没有找到唯一的关键参数。</exception>
        public static TDelegate Create <TDelegate>(object target, string id)
            where TDelegate : class
        {
            if (target == null)
            {
                throw CommonExceptions.ArgumentNull("target");
            }
            if (string.IsNullOrEmpty(id))
            {
                throw CommonExceptions.StringEmpty("id");
            }
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            if (!typeof(TDelegate).IsSubclassOf(typeof(Delegate)))
            {
                throw CommonExceptions.MustBeDelegate("TDelegate", typeof(TDelegate));
            }
            ProcessorData data = GetMethods <TDelegate>(target.GetType(), id, false);

            return(CreateSwitcher <TDelegate>(data, id, target));
        }
Exemplo n.º 4
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));
        }