.Net AJAX操作与反射的结合应用

  曾经在使用.Net中调用Ajax的时候会专门写一个页面,然后通过OperAjax(int ajaxOperType){...}来进行操作。

在这个函数当中使用大量的switch case 语句。从而使这个函数过于臃肿。难以维护,扩展,可读性也很差。

代码
  protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["Mode"] == null)
{
// 错误处理
return;
}

int ajaxOperType = Convert.ToInt32(Request.Params["Mode"].ToString());
this.OperAjax(ajaxOperType);
}
/// <summary>
/// 原始处理方式。
/// </summary>
/// <param name="ajaxOperType">执行类型</param>
private void OperAjax(int ajaxOperType)
{
string returnValue = "";
switch (ajaxOperType)
{
// 获取用户名执行的函数
case (int)Core.Enum.AjaxOperType.GetUserName:
returnValue
+= "{ Result : '关羽'}";
Response.Write(returnValue);
break;
// 判断姓名是否存在执行的函数
case (int)Core.Enum.AjaxOperType.ExistsUserName:
if (Request.Params["Value"] != null && Request.Params["Value"].ToString() == "貂蝉")
{
returnValue
+= "{ Result : 'true'}";
}
else
{
returnValue
+= "{ Result : 'false'}";
}
Response.Write(returnValue);
break;
default:
break;
}
}

  改进

  但是如果我们可以通过反射,可以帮助我们动态执行需要被调用的函数,这样就解决了代码判断过长,难维护,不易读的问题。

首先创建一个映射类,作用:给需要动态执行的AJAX函数打上标签。 

代码
  [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class AttributeAjax : System.Attribute
{
private AjaxOperType operType;
/// <summary>
/// 函数对应的ajax操作类型
/// </summary>
public AjaxOperType OperType
{
get
{
return this.operType;
}
set
{
this.operType = value;
}
}
}

  然后在写一下获取标签名称的反射类 

代码
  public class MappingAJAX
{
private static Hashtable hashTable = new Hashtable();

private static Hashtable InitialHashtable(ExecAjax entity)
{
if (hashTable != null && hashTable.Contains(entity.GetType().FullName))
return hashTable[entity.GetType().FullName] as Hashtable;

Hashtable entityHash
= new Hashtable();
MethodInfo[] methodInfo
= entity.GetType().GetMethods();
foreach (MethodInfo method in methodInfo)
{
object[] obj = method.GetCustomAttributes(typeof(AttributeAjax), false);
if (obj.Length > 0)
{
entityHash.Add(((AttributeAjax)obj[
0]).OperType, method.Name);
}
}
hashTable.Add(entity.GetType().FullName, entityHash);

return entityHash;
}

public static string GetMethodName(ExecAjax entity, AjaxOperType property)
{
Hashtable hashTable
= InitialHashtable(entity);
if (hashTable.Contains(property))
{
return (string)hashTable[property];
}

return null;
}
}

  前期工作都准备好了。剩下的就是写具体在AJAX当中需要执行的函数了。 

代码
  public class ExecAjax : System.Web.UI.Page
{
[AttributeAjax(OperType
= AjaxOperType.ExistsUserName)]
public string ExistsUserName(NameValueCollection ajaxValue)
{
string returnValue = "";
if (ajaxValue["Value"] != null && ajaxValue["Value"].ToString() == "貂蝉")
{
returnValue
+= "{ Result : 'true'}";
}
else
{
returnValue
+= "{ Result : 'false'}";
}
return returnValue;
}

[AttributeAjax(OperType
= AjaxOperType.GetUserName)]
public string GetUserName(NameValueCollection ajaxValue)
{
string returnValue = "{ Result : '关羽'}";
return returnValue;
}
}

  最后在具体页面调用的时候就可以写成这样: 

 

代码

public partial class Common_AJAXCommon : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["Mode"] == null)
{
// 错误处理
return;
}

int ajaxOperType = Convert.ToInt32(Request.Params["Mode"].ToString());
this.OperAjax(ajaxOperType);
}

/// <summary>
/// 反射处理方式。
/// </summary>
/// <param name="ajaxOperType">执行类型</param>
private void OperAjax(int ajaxOperType)
{
ExecAjax exec
= new ExecAjax();
string methodName = MappingAJAX.GetMethodName(exec, (AjaxOperType)ajaxOperType);
object[] objList = { Request.Params };
object o = exec.GetType().InvokeMember(methodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, exec, objList);
Response.Write(o.ToString());
}
}

总结:反射技术的使用可以减轻AJAXCommon.aspx页面处理函数过长,功能扩展需要维护的问题。

下载源代码:文件下载

posted @ 2010-06-20 20:50  13路易的  阅读(734)  评论(1编辑  收藏  举报