public static bool GetValue(ref object value, PropertyInfo propertyInfo, string DataControlName) { if (propertyInfo.PropertyType == typeof(int)) { value = TryParse.StrToInt(value, 0); } else if (propertyInfo.PropertyType == typeof(decimal)) { value = TryParse.StrToDecimal(value, 0); } else if (propertyInfo.PropertyType == typeof(float)) { value = TryParse.StrToFloat(value, 0); } else if (propertyInfo.PropertyType == typeof(DateTime)) { value = TryParse.StrToDate(value, DateTime.MinValue); } else if (propertyInfo.PropertyType == typeof(string)) { value = TryParse.ToString(value, string.Empty); object[] EntityBaseAttr = propertyInfo.GetCustomAttributes(typeof(EntityBaseAttribute), false); if (EntityBaseAttr != null && EntityBaseAttr.Length > 0) { int maxLength = ((EntityBaseAttribute)EntityBaseAttr[0]).MaxLength; if (value.ToString().Length > maxLength) { MessageBoxHelper.ShowError(DataControlName + "超出最大长度" + maxLength); return(false); } } } return(true); }
/// <summary> /// 取两数相减结果 /// </summary> /// <param name="val1"></param> /// <param name="val2"></param> /// <param name="defValue"></param> /// <returns></returns> public static decimal NumberMinus(object val1, object val2, decimal defValue) { decimal value = TryParse.StrToDecimal(val1) - TryParse.StrToDecimal(val2); if (value < 0m) { return(defValue); } return(value); }
/// <summary> /// 取两数相除结果,除数为0返回0 /// </summary> /// <param name="val1"></param> /// <param name="val2"></param> /// <returns></returns> public static decimal NumberDivide(object val1, object val2) { if (TryParse.StrToDecimal(val2) == 0) { return(0); } decimal value = TryParse.StrToDecimal(val1) / TryParse.StrToDecimal(val2); return(value); }
/// <summary> /// 将对象转换成int类型,转换失败将返回defValue /// </summary> /// <param name="Expression"></param> /// <param name="defValue"></param> /// <returns></returns> public static int StrToInt(object Expression, int defValue) { if (Expression == null) { return(defValue); } string input = Expression.ToString().Trim(); if (input.IndexOf(".") != -1) { input = TryParse.StrToDecimal(Expression).ToString("f0"); } if (((input.Length <= 0) || (input.Length > 11)) || !Regex.IsMatch(input, @"^[-]?\d*[.]?\d*$")) { return(defValue); } if (((input.Length >= 10) && ((input.Length != 10) || (input[0] != '1'))) && (((input.Length != 11) || (input[0] != '-')) || (input[1] != '1'))) { return(defValue); } return(Convert.ToInt32(input)); }