Esempio n. 1
0
 /// <summary>
 /// 初始化方法字典。
 /// </summary>
 /// <param name="methods">方法列表。</param>
 private void InitMethods(Delegate[] methods)
 {
     methodDict = new Dictionary <Type, TDelegate>(methods.Length);
     for (int i = 0; i < methods.Length; i++)
     {
         if (methods[i] == null)
         {
             throw CommonExceptions.ArgumentNull("methods[" + i + "]");
         }
         TDelegate dlg = methods[i].Wrap <TDelegate>();
         if (dlg == null)
         {
             throw CommonExceptions.DelegateCompatible("methods[" + i + "]", typeof(TDelegate));
         }
         methodDict.Add(methods[i].GetType().GetMethod("Invoke").GetParameters()[keyIndex].ParameterType, dlg);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 检查委托类型是否与处理器兼容。
        /// </summary>
        /// <typeparam name="TDelegate">调用委托的类型。</typeparam>
        /// <param name="data">处理器的数据。</param>
        /// <exception cref="ArgumentException">委托类型与处理器不匹配。</exception>
        private static void CheckDelegateType <TDelegate>(ProcessorData data)
        {
            Contract.Requires(data != null);
            Type dlgType = typeof(TDelegate);

            if (data.IsStatic)
            {
                if (data.DelegateType != dlgType)
                {
                    // 检查静态委托参数。
                    ParameterInfo[] paramInfos    = data.DelegateType.GetMethod("Invoke").GetParametersNoCopy();
                    ParameterInfo[] dlgParamInfos = dlgType.GetMethod("Invoke").GetParametersNoCopy();
                    if (paramInfos.Length != dlgParamInfos.Length)
                    {
                        throw CommonExceptions.DelegateCompatible(data.DelegateType, dlgType);
                    }
                    if (paramInfos.Where((param, idx) => !param.ParameterType.IsExplicitFrom(dlgParamInfos[idx].ParameterType))
                        .Any())
                    {
                        throw CommonExceptions.DelegateCompatible(data.DelegateType, dlgType);
                    }
                }
            }
            else
            {
                // 检查实例委托参数,要考虑实例对应的参数。
                ParameterInfo[] paramInfos    = data.DelegateType.GetMethod("Invoke").GetParametersNoCopy();
                ParameterInfo[] dlgParamInfos = dlgType.GetMethod("Invoke").GetParametersNoCopy();
                if (paramInfos.Length != dlgParamInfos.Length + 1)
                {
                    throw CommonExceptions.DelegateCompatible(data.DelegateType, dlgType);
                }
                for (int i = 1; i < paramInfos.Length; i++)
                {
                    if (!paramInfos[i].ParameterType.IsExplicitFrom(dlgParamInfos[i - 1].ParameterType))
                    {
                        throw CommonExceptions.DelegateCompatible(data.DelegateType, dlgType);
                    }
                }
            }
        }
Esempio n. 3
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 CommonExceptions.ProcessorNotFound("type", type, id);
                }
                bool isStatic = list[0].IsStatic;
                for (int i = 1; i < cnt; i++)
                {
                    if (list[i].IsStatic != isStatic)
                    {
                        throw CommonExceptions.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 CommonExceptions.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 CommonExceptions.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 CommonExceptions.DelegateCompatible("TDelegate", dlgType);
                }
                for (int i = 0; i < paramInfos.Length; i++)
                {
                    if (paramInfos[i].ParameterType != dlgParamInfos[i].ParameterType)
                    {
                        throw CommonExceptions.DelegateCompatible("TDelegate", dlgType);
                    }
                }
            }
            return(data.Item3);
        }