【.NET】Handler中使用反射分離程式碼
阿新 • • 發佈:2018-12-04
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
if (context.Request["ID"] != null)
{
string ID = context.Request["ID"].ToString();
System. Reflection.MethodInfo method = this.GetType().GetMethod("Method"+ID);
if (method != null)
{
method.Invoke(this, new object[] { context });
}
}
}
public bool IsReusable {
get {
return false;
}
}
public void Method1(HttpContext context)
{
context.Response.Write("Hello1");
}
public void Method2(HttpContext context)
{
context.Response.Write("Hello2");
}
}