编写轻量ajax组件第三篇实现(6)

  GetParameterFromRequest用于从请求获取object[]参数数组。根据上面所说的,如果参数是一个简单类型,那么直接进行转换;如果是实例对象,那么我们先要创建new一个实例对象,然后为其字段或属性赋值。实现如下:

/// <summary> /// 从请求获取参参数 /// </summary> /// <param>HttpRequest</param> ///<param>参数信息</param> /// <returns>参数数组</returns> private static object[] GetParametersFromRequest(HttpRequest request, ParameterInfo[] parameters) { if (parameters.IsNullOrEmpty()) { return null; } int length = parameters.Length; object[] realParameters = new object[length]; for (int i = 0; i < length; i++) { ParameterInfo pi = parameters[i]; Type piType = pi.ParameterType.GetRealType(); object value = null; if (piType.IsValueType()) { //值类型 value = ModelUtil.GetValue(request, pi.Name, piType); value = value ?? Activator.CreateInstance(piType); } else if (piType.IsClass) { //引用类型 object model = ModelUtil.CreateModel(piType); ModelUtil.FillModelByRequest(request, pi.Name, piType, model); value = model; } else { throw new NotSupportedException(pi.Name + " 参数不被支持"); } realParameters[i] = value; } return realParameters; }

  ModelUtil会从Http Request获取参数,并进行类型转换处理:

internal static class ModelUtil { /// <summary> /// 缓存构造函数 /// </summary> private static Hashtable constructorTable = Hashtable.Synchronized(new Hashtable()); /// <summary> /// 根据名称从HttpRequest获取值 /// </summary> /// <param>HttpRequest</param> /// <param>键名称</param> /// <param>参数类型</param> /// <returns></returns> public static object GetValue(HttpRequest request, string name, Type type) { string[] values = null; if (string.Compare(request.RequestType, "POST", true) == 0) { values = request.Form.GetValues(name); } else { values = request.QueryString.GetValues(name); } if (values.IsNullOrEmpty()) { return null; } string data = values.Length == 1 ? values[0] : string.Join(",", values); return Convert.ChangeType(data, type); } /// <summary> /// 创建实例对象 /// </summary> /// <param>实例类型</param> /// <returns></returns> public static object CreateModel(Type type) { if (type == null) { throw new ArgumentNullException("type"); } Func<object> func = constructorTable[type.AssemblyQualifiedName] as Func<object>; if (func == null) { func = ReflectionUtil.GetConstructorDelegate(type); constructorTable[type.AssemblyQualifiedName] = func; } if (func != null) { return func(); } return null; } /// <summary> /// 填充模型 /// </summary> /// <param>HttpRequest</param> /// <param>键名称</param> /// <param>参数类型</param> /// <parparam>实例对象</parparam> public static void FillModelByRequest(HttpRequest request, string name, Type type, object model) { if (model == null) { return; } IEnumerable<DataMember> members = DataMemberManager.GetDataMember(type); if (members.IsNullOrEmpty()) { return; } object value = null; foreach (DataMember member in members) { value = GetValue(request, string.Format("{0}.{1}", name, member.Name), member.MemberType); value = value ?? GetValue(request, member.Name, member.MemberType); member.SetValue(model, value); } } }

  如果是引用类型,需要通过构造函数创建对象,像前面用于,这里我们也用Expression来构建一个Func<object>类型的委托来优化,它调用了ReflectionUtil.GetConstructorDelegate方法。实现如下:

/// <summary> /// 获取构造函数委托 /// </summary> /// <param>实例类型</param> /// <returns></returns> public static Func<object> GetConstructorDelegate(Type type) { if (type == null) { throw new ArgumentNullException("type"); } ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes); if (ci == null) { throw new MissingMemberException("类型必须有一个无参public构造函数!"); } NewExpression newExp = Expression.New(type); Expression<Func<object>> lambda = Expression.Lambda<Func<object>>(newExp); return lambda.Compile(); }

  最后再输出结果时,如果是Get请求,并且需要缓存,我们还需要设置一下Response.Cache。如下:

private static void EndRequest(HttpContext context, object data, int outPutCache, ContentType contentType) { HttpResponse response = context.Response; if (outPutCache != 0) { if (string.Compare(context.Request.HttpMethod, "GET", true) == 0) { if (outPutCache > 0) { response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.SetMaxAge(new TimeSpan(0, 0, outPutCache)); response.Cache.SetExpires(DateTime.Now.AddSeconds(outPutCache)); } else { response.Cache.SetCacheability(HttpCacheability.NoCache); response.Cache.SetNoStore(); } } } response.ContentType = GetContentType(contentType); response.ContentEncoding = System.Text.Encoding.UTF8; if (data != null) { response.Write(data); } response.End(); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjppjy.html