//验证字符窜处理相关... private static bool validatedString(PropertyValidatedInfo colPropertyInfo, object inputValue) { bool b = true; if (colPropertyInfo.MaxLength > 0) { b = MB.Util.DataValidated.Instance.ValidatedLength(colPropertyInfo.MaxLength, inputValue.ToString()); if (b == false) { //如果检验不通过输出错误的信息 int leng = colPropertyInfo.MaxLength / 2; var errMsg = Message(MSG_CHECKED_LENGTH, new string[] { colPropertyInfo.Description, colPropertyInfo.MaxLength.ToString(), leng.ToString() }); throw new MB.Util.APPException(errMsg, APPMessageType.DataInvalid); } } if (colPropertyInfo.FitLength > 0) { b = MB.Util.DataValidated.Instance.ValidatedFitLength(colPropertyInfo.FitLength, inputValue.ToString()); if (!b) { var errMsg = Message(MSG_CHECKED_FIT, new string[] { colPropertyInfo.Description, colPropertyInfo.FitLength.ToString() }); throw new MB.Util.APPException(errMsg, APPMessageType.DataInvalid); } } return(b); }
//验证数字 private static bool validatedNumber(PropertyValidatedInfo colPropertyInfo, object inputValue) { double val = MB.Util.MyConvert.Instance.ToDouble(inputValue); //判断最大最小值 if (val < colPropertyInfo.MinValue || val > colPropertyInfo.MaxValue) { var errMsg = Message(MSG_MIN_MAX_VALUE, colPropertyInfo.Description, colPropertyInfo.MinValue.ToString(), colPropertyInfo.MaxValue.ToString()); throw new MB.Util.APPException(errMsg, APPMessageType.DataInvalid); } //判断小数点位数 if (colPropertyInfo.MaxDecimalPlaces >= 0) { string sv = val.ToString(); if (sv.IndexOf('.') >= 0) { int position = sv.Length - sv.LastIndexOf('.') - 1; if (position > colPropertyInfo.MaxDecimalPlaces) { var errMsg = Message(MSG_MAX_PLACES, colPropertyInfo.Description, colPropertyInfo.MaxDecimalPlaces.ToString()); throw new MB.Util.APPException(errMsg, APPMessageType.DataInvalid); } } } return(true); }
//创建一个验证信息。 private PropertyValidatedInfo createInfo(Expression <Func <T, object> > columnName, out PropertyInfo colInfo) { var colName = MB.Util.Expressions.ExpressionHelper.GetPropertyName <T>(columnName); if (string.IsNullOrEmpty(colName)) { throw new MB.Util.APPException(string.Format("根据表达式{0} 获取不到属性的名称,请检查", columnName), Util.APPMessageType.SysErrInfo); } if (this.ContainsKey(colName)) { throw new MB.Util.APPException(string.Format("当前集合中已经包含属性{0}", colName), Util.APPMessageType.SysErrInfo); } var temp = typeof(T).GetProperty(colName); if (temp == null) { throw new MB.Util.APPException(string.Format("根据表达式{0} 获取不到属性的名称,请检查是否为public property", columnName), Util.APPMessageType.SysErrInfo); } colInfo = temp; var newInfo = new PropertyValidatedInfo() { Name = colName }; this.Add(colInfo.Name, newInfo); if (_CreateAC) { Util.Emit.DynamicPropertyAccessor ac = new Util.Emit.DynamicPropertyAccessor(typeof(T), colInfo); _QuickDataAccess[colInfo.Name] = ac; } return(newInfo); }
/// <summary> /// 根据指定的ColumnPropertyInfo 判断输入的值是否符合要求。 /// </summary> /// <param name="colPropertyInfo"></param> /// <param name="inputValue"></param> /// <param name="errMsg"></param> /// <returns></returns> public static bool DataValidated(PropertyValidatedInfo colPropertyInfo, object inputValue) { bool b; //得到要检验的这个列 //如果是字符窜类型需要检验长度 //如果值为空不需要检验数据类型 if (colPropertyInfo.IsNull == true && (inputValue == null || inputValue.ToString().Length == 0)) { return(true); } if (colPropertyInfo.IsNull != true && (inputValue == null || inputValue.ToString().Length == 0)) { string nameDesc = (colPropertyInfo.Description != null && colPropertyInfo.Description.Length > 0) ? colPropertyInfo.Description : colPropertyInfo.Name; throw new MB.Util.APPException(string.Format(MSG_NOTNULL, nameDesc), APPMessageType.DataInvalid); } Type sType = colPropertyInfo.DataType; b = MB.Util.DataValidated.Instance.ValidatedDataType(sType, inputValue); if (b == true) { if (string.Compare(sType.Name, "String", true) == 0) { b = validatedString(colPropertyInfo, inputValue); } else if (Array.IndexOf(NUMBER_DATA_TYPE, sType.Name.ToUpper()) >= 0) { b = validatedNumber(colPropertyInfo, inputValue); } else { //其它类型先不做处理 后期根据实际需要再加上 } } else { throw new MB.Util.APPException(Message(MSG_MUST_INPUT, new string[] { colPropertyInfo.Description, MB.Util.DataValidated.Instance.GetTypeValidatedErrMsg(sType) }), APPMessageType.DataInvalid); } return(b); }