示例#1
0
 public ActionDescription(MethodInfo m, ActionAttribute atrr)
     : base(m)
 {
     this.MethodInfo = m;
     this.Attr       = atrr;
     this.Parameters = m.GetParameters();
     this.HasReturn  = m.ReturnType != ReflectionHelper.VoidType;
 }
示例#2
0
        /// <summary>
        /// 加载所有的Controller
        /// </summary>
        private static void InitControllers()
        {
            BaseActionHandlerFactory[] baseActionHandlerFactoryList = GetConfigBaseActionHandlerFactory();

            List <ControllerDescription> serviceControllerList = new List <ControllerDescription>(1024);
            var pageControllerList = new List <ControllerDescription>(1024);

            ICollection assemblies = BuildManager.GetReferencedAssemblies();

            foreach (Assembly assembly in assemblies)
            {
                // 过滤以【System】开头的程序集,加快速度
                if (assembly.FullName.StartsWith("System", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                foreach (Type t in assembly.GetExportedTypes())
                {
                    if (t.IsClass == false)
                    {
                        continue;
                    }

                    if (t.Name.EndsWith("Controller"))
                    {
                        pageControllerList.Add(new ControllerDescription(t));
                    }

                    for (int i = 0; i < baseActionHandlerFactoryList.Length; i++)
                    {
                        if (baseActionHandlerFactoryList[i].TypeIsService(t))
                        {
                            serviceControllerList.Add(new ControllerDescription(t));
                            break;
                        }
                    }
                }
            }

            // 用于Ajax or Service 调用的Action信息则采用延迟加载的方式。

            s_ServiceFullNameDict = serviceControllerList.ToDictionary(x => x.ControllerType.FullName, StringComparer.OrdinalIgnoreCase);

            s_ServiceShortNameDict = new Dictionary <string, ControllerDescription>(s_ServiceFullNameDict.Count, StringComparer.OrdinalIgnoreCase);

            foreach (ControllerDescription description in serviceControllerList)
            {
                try {
                    s_ServiceShortNameDict.Add(description.ControllerType.Name, description);
                }
                catch (ArgumentException) {
                    // 如果遇到已存在的KEY,把原先存放的项也设置为 null,便于查找时返回 null
                    // 也就是说:短名不能重复,如果重复则不返回任何一个。
                    s_ServiceShortNameDict[description.ControllerType.Name] = null;
                }
            }



            // 提前加载Page Controller中的所有Action方法
            s_PageActionDict = new Dictionary <string, ActionDescription>(4096, StringComparer.OrdinalIgnoreCase);

            List <RegexActionDescription> regexActions = new List <RegexActionDescription>();


            foreach (ControllerDescription controller in pageControllerList)
            {
                foreach (MethodInfo m in controller.ControllerType.GetMethods(ActionBindingFlags))
                {
                    ActionAttribute actionAttr = m.GetMyAttribute <ActionAttribute>();
                    if (actionAttr != null)
                    {
                        ActionDescription actionDescription =
                            new ActionDescription(m, actionAttr)
                        {
                            PageController = controller
                        };

                        PageUrlAttribute[] pageUrlAttrs = m.GetMyAttributes <PageUrlAttribute>();

                        foreach (PageUrlAttribute attr in pageUrlAttrs)
                        {
                            if (string.IsNullOrEmpty(attr.Url) == false)
                            {
                                s_PageActionDict.Add(attr.Url, actionDescription);
                            }
                        }



                        PageRegexUrlAttribute[] regexAttrs = m.GetMyAttributes <PageRegexUrlAttribute>();
                        foreach (PageUrlAttribute attr2 in regexAttrs)
                        {
                            if (string.IsNullOrEmpty(attr2.Url) == false)
                            {
                                Regex regex = new Regex(attr2.Url, RegexOptions.Compiled);
                                regexActions.Add(new RegexActionDescription {
                                    Regex = regex, ActionDescription = actionDescription
                                });
                            }
                        }
                    }
                }
            }


            if (regexActions.Count > 0)
            {
                s_RegPageActionArray = regexActions.ToArray();
            }
        }