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); }
/// <summary> /// 序列化 /// </summary> /// <param name="value">值</param> /// <param name="type">返回序列化类型</param> /// <param name="compressionThreshold">指定超过长度时启用压缩功能</param> /// <returns></returns> public static byte[] Serialize(object value, out SerializedType type, uint compressionThreshold) { byte[] bytes; if (value is byte[]) { bytes = (byte[])value; type = SerializedType.ByteArray; if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedByteArray; } } else if (value is string) { bytes = Encoding.UTF8.GetBytes((string)value); type = SerializedType.String; if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedString; } } else if (value is DateTime) { bytes = BitConverter.GetBytes(((DateTime)value).Ticks); type = SerializedType.Datetime; } else if (value is bool) { bytes = new byte[] { (byte)((bool)value ? 1 : 0) }; type = SerializedType.Bool; } else if (value is byte) { bytes = new byte[] { (byte)value }; type = SerializedType.Byte; } else if (value is short) { bytes = BitConverter.GetBytes((short)value); type = SerializedType.Short; } else if (value is ushort) { bytes = BitConverter.GetBytes((ushort)value); type = SerializedType.UShort; } else if (value is int) { bytes = BitConverter.GetBytes((int)value); type = SerializedType.Int; } else if (value is uint) { bytes = BitConverter.GetBytes((uint)value); type = SerializedType.UInt; } else if (value is long) { bytes = BitConverter.GetBytes((long)value); type = SerializedType.Long; } else if (value is ulong) { bytes = BitConverter.GetBytes((ulong)value); type = SerializedType.ULong; } else if (value is float) { bytes = BitConverter.GetBytes((float)value); type = SerializedType.Float; } else if (value is double) { bytes = BitConverter.GetBytes((double)value); type = SerializedType.Double; } else { type = SerializedType.ObjectJson; Type t = value.GetType(); switch (ReflectTool.GetSystemType(ref t)) { case SysType.Base: case SysType.Enum: case SysType.Custom: if (t.GetCustomAttributes(typeof(SerializableAttribute), false).Length > 0) { type = SerializedType.Object; } break; } if (type == SerializedType.Object) { //可序列化 List<object> 如果 object 不支持序列化,会挂。 using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); bytes = ms.ToArray(); if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedObject; } } } else { string json = JsonHelper.ToJson(value); bytes = Encoding.UTF8.GetBytes(json); if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedObjectJson; } } } return(bytes); }
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); }