佛山软件定制

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

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

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

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

实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* *
 * 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赋值:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace AtNet.Web.Tools
{
    using System;
    using System.Reflection;
 
    public class WebHandler:AtNet.Web.ExecuteHandler
    {
        static WebHandler()
        {
           _type = typeof(WebHandler);
       }
    }
}

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

posted on   New.min  阅读(780)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

点击右上角即可分享
微信分享提示