• 更灵活,更易维护的WebHandler之通用webHandler编码方案(2)


    上一篇:更灵活,更易维护的WebHandler之通用webHandler编码方案(1) 中介绍了在同一个程序集中使用webHandler执行类的方法,

    但在多数情况下,我们会将WebHandler封装进一个单独的动态链接库,我们需要将引用WebHandler DLL的程序集或程序集中的任意一个类的Type做为参数传递给WebHandler,

    这样就可以在WebHandler中利用反射调用引用WebHandler类库的程序集中的代码!

    实现代码如下:

    /* *
     * name     : ExecuteHandler.cs 
     * author   : newmin
     * date     : 09/29 2010
     * note     : 用来处理请求,请求的URI参数如:Exc.ashx?cmd=IP,GetIP,127.0.0.1
     * 
     * 要执行操作的类必需要程序集名称命名空间下:
     * 如要执行AtNet.Security下的User类,则User类的命名空间为:HuiShi.Security.User
     * 调用方式**.ashx?cmd=User,GetScore,newmin
     * 
     * */
    namespace AtNet.Web
    {
        using System;
        using System.Web;
        using System.Reflection;
        using System.Collections.Generic;
    
        public abstract class ExecuteHandler : IHttpHandler
        {
            //绑定类型用于获取程序集,只能在子类的静态构造函数中赋值
            protected static Type _type;
            #region IHttpHandler 成员
            public bool IsReusable{ get; set; }
            
            public void ProcessRequest(HttpContext context)
            {
                string cmd=context.Request["cmd"].Replace("+"," ");         //将空格做为+号替换
    
                string[] args = cmd.Split(',');
                if (args.Length > 2)
                {
                    //获取执行当前代码的程序集并创建实例
                   Assembly ass = Assembly.GetAssembly(_type);
                    object obj = ass.CreateInstance(_type.Namespace+"."+args[0], true);
    
                    //获取实例类型
                    Type type=obj.GetType();
    
                    //未添加WebExecuteAttribute特性的类将不被执行
                    object[] attrs= type.GetCustomAttributes(typeof(WebExecuteAttribute), false);
                    WebExecuteAttribute attr =attrs.Length>0?attrs[0] as WebExecuteAttribute:null;
                    if (attr == null) { context.Response.Write("此模块不允许被执行!"); return; }
    
                    //获取方法并执行
                    MethodInfo method =type.GetMethod(args[1],BindingFlags.Instance|BindingFlags.Public|BindingFlags.IgnoreCase);
                    object returnObj=method.GetParameters() != null?method.Invoke(obj,cmd.Substring(args[0].Length + args[1].Length + 2).Split(','))
                            :method.Invoke(obj, null);
    
                    //如国返回String类型或值类型则输出到页面
                    if (method.ReturnType == typeof(string) ||obj is ValueType)
                        context.Response.Write(returnObj.ToString());
                }
            }
    
            #endregion
        }
    }
    
    

    我们需在继承ExecuteHandler的类的静态构造函数中对_type赋值:

    namespace AtNet.Web.Tools
    {
        using System;
        using System.Reflection;
    
        public class WebHandler:AtNet.Web.ExecuteHandler
        {
            static WebHandler()
            {
               _type = typeof(WebHandler);
           }
        }
    }
    

     这样我们就能在将ExecuteHandler分离出来,被别的项目所引用

  • 相关阅读:
    C# 学习之旅(1)
    SqlServer安装教程
    Aop和Filter区别
    Redis入门
    SpringMVC与Struts2的主要区别
    SpringMVC执行流程
    ssm初始化环境搭建
    Oracle 高效分页
    Oracle 综合:游标和动态SQL
    Oracle 动态SQL
  • 原文地址:https://www.cnblogs.com/newmin/p/1846357.html
Copyright © 2020-2023  润新知