private bool GetInvokeParas(MethodInfo method, out object[] paras) { paras = null; #region 增加处理参数支持 ParameterInfo[] piList = method.GetParameters(); object[] validateList = method.GetCustomAttributes(typeof(ValidateAttribute), true); if (piList != null && piList.Length > 0) { paras = new object[piList.Length]; for (int i = 0; i < piList.Length; i++) { ParameterInfo pi = piList[i]; Type t = pi.ParameterType; string value = Query <string>(pi.Name, null); if (value == null) { if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable")) { continue; } if (ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型 { value = GetJson(); } } //检测是否允许为空,是否满足正则格式。 if (!ValidateParas(validateList, pi.Name, value)) { return(false); } try { paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体) } catch (Exception err) { string typeName = t.Name; if (typeName.StartsWith("Nullable")) { typeName = Nullable.GetUnderlyingType(t).Name; } string outMsg = string.Format("[{0} {1} = {2}] [Error : {3}]", typeName, pi.Name, value, err.Message); Write(outMsg, false); return(false); } } } #endregion return(true); }
private object[] GetInvokeParas(MethodInfo method) { object[] paras = null; #region 增加处理参数支持 ParameterInfo[] piList = method.GetParameters(); if (piList != null && piList.Length > 0) { paras = new object[piList.Length]; for (int i = 0; i < piList.Length; i++) { ParameterInfo pi = piList[i]; Type t = pi.ParameterType; string value = Query <string>(pi.Name, null); if (value == null) { if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable")) { continue; } if (ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型 { value = GetJson(); } } try { paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体) } catch (Exception err) { string typeName = t.Name; if (typeName.StartsWith("Nullable")) { typeName = Nullable.GetUnderlyingType(t).Name; } string outMsg = string.Format("[{0} {1} = {2}] [Error : {3}]", typeName, pi.Name, value, err.Message); Write(outMsg, false); // context.Response.Write(outMsg); context.Response.End(); break; } } } #endregion return(paras); }
private bool GetInvokeParas(MethodInfo method, out object[] paras) { paras = null; #region 增加处理参数支持 ParameterInfo[] piList = method.GetParameters(); object[] validateList = method.GetCustomAttributes(typeof(RequireAttribute), true); if (piList != null && piList.Length > 0) { paras = new object[piList.Length]; for (int i = 0; i < piList.Length; i++) { ParameterInfo pi = piList[i]; Type t = pi.ParameterType; object value = Query <object>(pi.Name, null); if (value == null) { if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable")) { continue; } if (t.Name == "HttpPostedFile") { if (Request.Files != null && Request.Files.Count == 1) { value = Request.Files[0]; } } else if (piList.Length == 1 && ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型 { value = GetJson(); } } //检测是否允许为空,是否满足正则格式。 if (!ValidateParas(validateList, pi.Name, Convert.ToString(value))) { return(false); } try { //特殊值处理 if (t.Name == "HttpPostedFile" && value is string && Convert.ToString(value) == DocSettings.DocDefaultImg.ToLower()) { string path = DocSettings.DefaultImg; if (!string.IsNullOrEmpty(path)) { paras[i] = HttpPostedFileExtend.Create(path); } } else { paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体) } } catch (Exception err) { string typeName = t.Name; if (typeName.StartsWith("Nullable")) { typeName = Nullable.GetUnderlyingType(t).Name; } string outMsg = string.Format("[{0} {1} = {2}] [Error : {3}]", typeName, pi.Name, value, err.Message); Write(outMsg, false); return(false); } } } //对未验证过的参数,再进行一次验证。 foreach (object item in validateList) { RequireAttribute valid = item as RequireAttribute; if (!valid.isValidated) { if (valid.paraName.IndexOf(',') > -1) { foreach (string name in valid.paraName.Split(',')) { if (string.IsNullOrEmpty(Query <string>(name))) { Write(string.Format(valid.emptyTip, name), false); return(false); } } } else if (!ValidateParas(validateList, valid.paraName, Query <string>(valid.paraName))) { return(false); } } } validateList = null; #endregion return(true); }