Esempio n. 1
0
        /// <summary>
        /// Add all controllers found in the specified assembly.
        /// </summary>
        /// <param name="assembly"></param>
        /// <remarks>It will also look for views in a subfolder to the controller.</remarks>
        public void LoadControllers(Assembly assembly)
        {
            Type controllerType = typeof(Controller);

            foreach (Type type in assembly.GetTypes())
            {
                if (type.IsAbstract || type.IsInterface)
                {
                    continue;
                }
                if (!controllerType.IsAssignableFrom(type))
                {
                    continue;
                }

                // find default constructor
                bool found = false;
                foreach (ConstructorInfo info in type.GetConstructors())
                {
                    if (info.GetParameters().Length != 0)
                    {
                        continue;
                    }
                    found = true;
                    break;
                }
                if (!found)
                {
                    continue;
                }

                // load controller.
                var instance = (Controller)Activator.CreateInstance(type);
                _server.Add(instance);

                // load resources in controller subfolder Views and map them to "/controllerName/".
                _resource.AddFilesInFolder(instance.ControllerUri, type.Assembly, type.Namespace + ".Views");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Add all controllers found in the specified assembly.
        /// </summary>
        /// <param name="assembly"></param>
        /// <remarks>It will also look for views in a subfolder to the controller.</remarks>
        public void LoadControllers(Assembly assembly)
        {
            Type controllerType = typeof(Controller);

            foreach (Type type in assembly.GetTypes())
            {
                if (type.IsAbstract || type.IsInterface)
                {
                    continue;
                }
                if (!controllerType.IsAssignableFrom(type) && type != controllerType)
                {
                    continue;
                }

                // find default constructor
                bool found = false;
                foreach (ConstructorInfo info in type.GetConstructors())
                {
                    if (info.GetParameters().Length != 0)
                    {
                        continue;
                    }
                    found = true;
                    break;
                }
                if (!found)
                {
                    continue;
                }

                _server.Register(type);
                var uri = _server.RoutingService.GetUriFor(type);
                _resource.AddFilesInFolder(uri, type.Assembly, type.Namespace + ".Views");
            }
        }