示例#1
0
文件: Route.cs 项目: 5l1v3r1/Maze-1
        public Route(RouteDescription description, Type controllerType, MethodInfo routeMethod, RouteType routeType)
        {
            Description    = description;
            ControllerType = controllerType;
            RouteMethod    = routeMethod;
            RouteType      = routeType;

            ActionInvoker = new Lazy <ActionInvoker>(() => new ActionInvoker(controllerType, routeMethod),
                                                     LazyThreadSafetyMode.ExecutionAndPublication);
        }
示例#2
0
        public void BuildCache(IReadOnlyDictionary <PackageIdentity, List <Type> > controllers)
        {
            var routes            = new Dictionary <RouteDescription, Route>();
            var channelBaseType   = typeof(MazeChannel);
            var channelInitMethod = channelBaseType.GetMethod(nameof(MazeChannel.Initialize),
                                                              BindingFlags.Instance | BindingFlags.Public);

#if NETCOREAPP
            foreach (var(package, types) in controllers)
            {
#else
            foreach (var keyValuePair in controllers)
            {
                var package = keyValuePair.Key;
                var types   = keyValuePair.Value;
#endif

                foreach (var controllerType in types)
                {
                    if (controllerType.IsAbstract)
                    {
                        continue;
                    }

                    var routeFragments = new List <IRouteFragment>();

                    var routeAttribute = controllerType.GetCustomAttribute <RouteAttribute>();
                    if (routeAttribute != null)
                    {
                        routeFragments.Add(routeAttribute);
                    }

                    var routeType = RouteType.Http;
                    if (controllerType.IsSubclassOf(channelBaseType))
                    {
                        var segments    = GetSegments(routeFragments, controllerType, methodInfo: null);
                        var description = new RouteDescription(package, "GET", segments);
                        routes.Add(description,
                                   new Route(description, controllerType, channelInitMethod, RouteType.ChannelInit));
                        routeType = RouteType.Channel;
                    }

                    foreach (var methodInfo in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
                    {
                        var methodPath = new List <IRouteFragment>(routeFragments);

                        var methodAttributes = methodInfo.GetCustomAttributes().ToList();

                        if (methodAttributes.OfType <NonActionAttribute>().Any() ||
                            methodAttributes.OfType <CompilerGeneratedAttribute>().Any())
                        {
                            continue;
                        }

                        var methodAttribute = methodAttributes.OfType <IActionMethodProvider>().FirstOrDefault();
                        if (methodAttribute == null)
                        {
                            continue;
                        }

                        var method = methodAttribute.Method;

                        foreach (var routeFragment in methodAttributes.OfType <IRouteFragment>())
                        {
                            methodPath.Add(routeFragment);
                        }

                        var segments    = GetSegments(methodPath, controllerType, methodInfo);
                        var description = new RouteDescription(package, method, segments);
                        routes.Add(description, new Route(description, controllerType, methodInfo, routeType));
                    }
                }
            }

            Routes = routes.ToImmutableDictionary();
        }