ASP.NET MVC 仿真 - (3)从Assemblies中找出所有的Controller

上次已经获取了Controller的名字,想通过Controller的名字来实例化一个Controller,该怎么办呢?

MVC框架的做法是从相关的Assembly中找出所有符合Controller特性的类型。将它们放入一个叫MVC-ControllerTypeCache.xml的缓存中。这次重点我们来仿真一下如何从所有先关的Assembly中找出Controller类型,缓存机制到以后性能优化再详细叙述。


IController.cs:

namespace MvcFake.mvc
{
    public interface IController
    {
    }
}

HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcFake.mvc;

namespace MvcFake.Controllers
{
    public class HomeController : IController
    {

    }
}

MvcHandler.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Collections;
using System.IO;
using System.Web.Compilation;
using System.Reflection;

namespace MvcFake.mvc
{
    public class MvcHandler : IHttpHandler
    {
        public bool IsReusable { get; set; }
        private RequestContext RequestContext { get; set; }

        public MvcHandler(RequestContext requestContext)
        {
            RequestContext = requestContext;
        }


        internal static bool IsControllerType(Type t)
        {
            return
                t != null &&
                t.IsPublic &&
                t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
                !t.IsAbstract &&
                typeof(IController).IsAssignableFrom(t);
        }

        private static bool TypeIsPublicClass(Type type)
        {
            return (type != null && type.IsPublic && type.IsClass && !type.IsAbstract);
        }

        private static IEnumerable<Type> FilterTypesInAssemblies()
        {
            IEnumerable<Type> typesSoFar = Type.EmptyTypes;

            ICollection assemblies = BuildManager.GetReferencedAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                Type[] typesInAsm;
                try
                {
                    typesInAsm = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex)
                {
                    typesInAsm = ex.Types;
                }
                typesSoFar = typesSoFar.Concat(typesInAsm);
            }
            return typesSoFar.Where(type => TypeIsPublicClass(type) && IsControllerType(type));
        }

        public void ProcessRequest(HttpContext context)
        {
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");

            context.Response.Write("Controller Name: " + controllerName);

            IEnumerable<Type> types = FilterTypesInAssemblies();

            foreach (var type in types)
            {
                context.Response.Write("<br>Controller Type Name: " + type.ToString());
            }
        }
    }
}

项目结构:


运行程序:


从运行的结果可以看到,我们已经成功地找到Assembly中的Controller了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值