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> /// yyyy-MM-dd -yyyyMMdd /// </summary> /// <param name="value">要转换的值</param> /// <param name="DateTimeFormat">转换格式</param> /// <returns></returns> public static DateTime StrToDateByChar(object value, string DateTimeFormat) { if (string.IsNullOrEmpty(TryParse.ToString(value))) { return(DateTime.Today); } try { return(DateTime.ParseExact(TryParse.ToString(value), DateTimeFormat, System.Globalization.CultureInfo.CurrentCulture)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 将固定格式(yyyy-MM-dd) 得到日期格式 yyyyMMdd /// </summary> /// <param name="value"></param> /// <returns></returns> public static string StrToDateByChar(object value) { if (string.IsNullOrEmpty(TryParse.ToString(value))) { return(""); } try { return(DateTime.ParseExact(TryParse.ToString(value), "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyyMMdd")); } catch (Exception ex) { throw ex; } }