using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ICSSoft.FromERP { public static class ConvertExt { public static char ToChar(this object data) { char returnValue = default(char); if (data == null) return returnValue; else { if (char.TryParse(data.ToString(), out returnValue)) return returnValue; else return returnValue; } } #region 文本转换 public static string ToStringExt(this object data) { return data == null ? string.Empty : data.ToString(); } #endregion #region 日期转换 /// /// 转换为日期 /// /// 数据 /// DateTime. public static DateTime ToDate(this object data) { try { if (data == null) return DateTime.MinValue; if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$")) { string strValue = data.ToStringExt(); return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt()); } DateTime result; return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue; } catch { return DateTime.MinValue; } } /// /// 转换为可空日期 /// /// 数据 /// System.Nullable<DateTime>. public static DateTime? ToDateOrNull(this object data) { try { if (data == null) return null; if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$")) { string strValue = data.ToStringExt(); return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt()); } DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (isValid) return result; return null; } catch { return null; } } /// /// 转换为日期 /// /// 数据 /// DateTime. public static DateTime ToDateTime(this object data) { try { if (data == null) return DateTime.MinValue; if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{14}$")) { string strValue = data.ToStringExt(); return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt(), strValue.Substring(8, 2).ToInt(), strValue.Substring(10, 2).ToInt(), strValue.Substring(12, 2).ToInt()); } DateTime result; return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue; } catch { return DateTime.MinValue; } } /// /// 转换为可空日期 /// /// 数据 /// System.Nullable<DateTime>. public static DateTime? ToDateTimeOrNull(this object data) { try { if (data == null) return null; if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{14}$")) { string strValue = data.ToStringExt(); return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt(), strValue.Substring(8, 2).ToInt(), strValue.Substring(10, 2).ToInt(), strValue.Substring(12, 2).ToInt()); } DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (isValid) return result; return null; } catch { return null; } } #endregion public static bool ToBool(this object data) { Boolean returnValue = false; if (data == null) return returnValue; else { if (Boolean.TryParse(data.ToString(), out returnValue)) return returnValue; else return returnValue; } } public static Nullable ToBoolWithNull(this object data) { Boolean returnValue = false; if (data == null) return null; else { if (Boolean.TryParse(data.ToString(), out returnValue)) return returnValue; else return null; } } #region 数字转换 public static decimal ToDecimal(this object data) { decimal returnValue = 0; if (data == null) return returnValue; else { if (decimal.TryParse(data.ToString(), out returnValue)) { return returnValue; } else { return 0; } } } public static Nullable ToDecimalWithNull(this object data) { decimal returnValue = 0; if (data == null) return null; else { if (decimal.TryParse(data.ToString(), out returnValue)) return returnValue; else return null; } } public static float ToFloat(this object data) { float returnValue = 0; if (data == null) return returnValue; else { if (float.TryParse(data.ToString(), out returnValue)) { return returnValue; } else { return returnValue; } } } public static Nullable ToFloatWithNull(this object data) { float returnValue = 0; if (data == null) return null; else { if (float.TryParse(data.ToString(), out returnValue)) return returnValue; else return null; } } public static double ToDouble(this object data) { double returnValue = 0; if (data == null) return returnValue; else { if (double.TryParse(data.ToString(), out returnValue)) { return returnValue; } else { return 0; } } } public static Nullable ToDoubleWithNull(this object data) { double returnValue = 0; if (data == null) return null; else { if (double.TryParse(data.ToString(), out returnValue)) return returnValue; else return null; } } public static Int64 ToInt64(this object data) { Int64 returnValue = 0; if (data == null) return returnValue; else { if (Int64.TryParse(data.ToString(), out returnValue)) return returnValue; else return 0; } } public static Nullable ToInt64WithNull(this object data) { Int64 returnValue = 0; if (data == null) return null; else { if (Int64.TryParse(data.ToString(), out returnValue)) return returnValue; else return null; } } public static Int32 ToInt(this object data) { Int32 returnValue = 0; if (data == null) return returnValue; else { if (Int32.TryParse(data.ToString(), out returnValue)) { return returnValue; } else { return 0; } } } public static Nullable ToIntWithNull(this object data) { if (data == null) return null; int result; bool isValid = int.TryParse(data.ToString(), out result); if (isValid) return result; return null; } public static Int16 ToInt16(this object data) { Int16 returnValue = 0; if (data == null) return returnValue; else { if (Int16.TryParse(data.ToString(), out returnValue)) { return returnValue; } else { return 0; } } } public static Nullable ToInt16WithNull(this object data) { Int16 returnValue = 0; if (data == null) return null; else { if (Int16.TryParse(data.ToString(), out returnValue)) return returnValue; else return null; } } public static bool IsNumeric(this object o) { if (o == null || o.ToString() == "") return false; string strNum = o.ToString(); System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^-?[0-9]*$"); return reg1.IsMatch(strNum); } #endregion } }