Example #1
0
        /// <summary>
        /// 返回与指定标识符相关的处理器方法集合。
        /// </summary>
        /// <typeparam name="TDelegate">使用基类型调用方法的委托。</typeparam>
        /// <param name="type">在其中查找静态或实例方法的类型。</param>
        /// <param name="id">方法切换器的标识符。</param>
        /// <param name="index">方法的关键参数索引。</param>
        /// <param name="queryStatic">是否请求的是静态方法。</param>
        /// <returns>与指定标识符相关的处理器方法集合。</returns>
        private static Dictionary <Type, Delegate> GetMethods <TDelegate>(Type type, string id, int index, bool queryStatic)
        {
            Type dlgType = typeof(TDelegate);
            Tuple <bool, Type, Dictionary <Type, Delegate> > data;
            string key = string.Concat(type.FullName, "_", id);

            if (!methodDict.TryGetValue(key, out data))
            {
                MethodInfo[]      methods = type.GetMethods(MethodFlags);
                List <MethodInfo> list    = new List <MethodInfo>();
                for (int i = 0; i < methods.Length; i++)
                {
                    if (methods[i].GetCustomAttributes(typeof(ProcessorAttribute), true)
                        .Cast <ProcessorAttribute>().Any(s => s.Id == id))
                    {
                        list.Add(methods[i]);
                    }
                }
                int cnt = list.Count;
                if (cnt == 0)
                {
                    throw ExceptionHelper.ProcessorNotFound("type", type, id);
                }
                bool isStatic = list[0].IsStatic;
                for (int i = 1; i < cnt; i++)
                {
                    if (list[i].IsStatic != isStatic)
                    {
                        throw ExceptionHelper.ProcessorMixed("type", type, id);
                    }
                }
                Dictionary <Type, Delegate> dict = new Dictionary <Type, Delegate>();
                Type newDlgType = dlgType;
                if (!isStatic)
                {
                    newDlgType = GetInstanceDlgType(newDlgType);
                }
                for (int i = 0; i < cnt; i++)
                {
                    Type     keyType = list[i].GetParameters()[index].ParameterType;
                    Delegate dlg     = DelegateBuilder.CreateDelegate(newDlgType, list[i], false);
                    if (dlg == null)
                    {
                        throw ExceptionHelper.DelegateCompatible(list[i].ToString(), dlgType);
                    }
                    dict.Add(keyType, dlg);
                }
                data = new Tuple <bool, Type, Dictionary <Type, Delegate> >(isStatic, dlgType, dict);
                methodDict.Add(key, data);
            }
            if (data.Item1 != queryStatic)
            {
                throw ExceptionHelper.ProcessorMismatch("type", type, id);
            }
            if (data.Item2 != dlgType)
            {
                // 检查委托参数。
                ParameterInfo[] paramInfos    = data.Item2.GetMethod("Invoke").GetParameters();
                ParameterInfo[] dlgParamInfos = dlgType.GetMethod("Invoke").GetParameters();
                if (paramInfos.Length != dlgParamInfos.Length)
                {
                    throw ExceptionHelper.DelegateCompatible("TDelegate", dlgType);
                }
                for (int i = 0; i < paramInfos.Length; i++)
                {
                    if (paramInfos[i].ParameterType != dlgParamInfos[i].ParameterType)
                    {
                        throw ExceptionHelper.DelegateCompatible("TDelegate", dlgType);
                    }
                }
            }
            return(data.Item3);
        }