using System; using System.Reflection; namespace CSFramework.DB { internal static class CommonMethods { internal static DateTime MinSqlDate => DateTime.Parse("1901-01-01 00:00"); /// /// 转换为SQL支持的日期格式, 日期范围:1753-1-1 ~ 9999-12-31 /// /// /// internal static DateTime ToSqlDateTime(object o) { var defMinValue = DateTime.Parse("1753-01-01 00:00:00"); var defMaxValue = DateTime.Parse("9999-12-31 23:59:59"); if (null == o) return defMinValue; //传入空值,返回预设值 DateTime dt; if (DateTime.TryParse(o.ToString(), out dt)) { if (dt < defMinValue || dt > defMaxValue) return defMinValue; //无效日期,预设返回SQL支持的最小日期 else return dt; } return defMinValue; } /// /// 转换为decimal类型(有效十进制数) /// /// /// public static decimal ToDecimal(object o) { if (null == o) return 0; try { return Convert.ToDecimal(o.ToString()); } catch { return 0; } } /// /// 获取对象的属性和成员变量 /// /// /// internal static FieldInfo[] GetAllInfos(Type T) { FieldInfo[] infos = null; var infosSelf = T.GetFields(); FieldInfo[] infosBase = null; if (T.BaseType != null) infosBase = GetAllInfos(T.BaseType); if (infosBase == null || infosBase.Length == 0) { infos = infosSelf; } else { var iLength = infosBase.Length; Array.Resize(ref infosBase, infosBase.Length + infosSelf.Length); infosSelf.CopyTo(infosBase, iLength); infos = infosBase; } return infos; } } }