using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Web.Security; using System.Security.Cryptography; using System.IO; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Reflection; using System.Data; public static class DHelper { #region 常用处理 /// /// 检查DataTable 是否有数据行 /// /// DataTable /// public static bool IsHaveRows(this System.Data.DataTable dt) { if (dt != null && dt.Rows.Count > 0) return true; return false; } /// /// 对象是否为NULL /// /// 对象 /// 是否为NULL public static bool IsNull(this object obj) { if ((Object.Equals(obj, null)) || (Object.Equals(obj, DBNull.Value)) || (obj == null)) { return true; } if (obj is DateTime && obj.ToDateTime().Equals(DateTime.MinValue)) { return true; } return false; } /// /// 是否包含str字符有true /// /// /// /// public static bool IsIndexOf(this string strs, string str) { if (strs.ToLower().IndexOf(str.ToLower()) != -1) return true; return false; } /// /// 字符串是否为空true是空 /// /// 字符串 /// 是否为空 public static bool IsEmpty(this string str) { if (IsNull(str)) { return true; } if (str.Equals(String.Empty)) { return true; } return false; } /// /// 对象不为NULL /// /// 字符串 /// 返回字符串 public static string IfNull(this object str) { if (str == null || Object.Equals(str, null)) { return String.Empty; } return str.ToString(); } /// /// 对象不为NULL /// /// 字符串 /// 返回字符串 public static bool IfNotNull(this object str) { if ((Object.Equals(str, null)) || (Object.Equals(str, DBNull.Value)) || (str == null)) { return false; } return true; } /// /// 有值?(与IsNullOrEmpty相反) /// /// public static bool IsValuable(this IEnumerable thisValue) { if (thisValue == null || thisValue.Count() == 0) return false; return true; } /// /// 字符串不为NULL /// /// 要验证字符串 /// 为空返回字符串 /// 返回字符串 public static string IfNull(this string str, string retStr) { if (str == null || Object.Equals(str, null)) { return retStr; } return str; } /// /// 字符串数组转换成数字 /// /// 字符串 /// 数字 public static int[] StrsToInts(this string[] strs) { int[] ints = new int[strs.Length]; for (int i = 0; i < strs.Length; i++) { ints[i] = Convert.ToInt32(strs[i]); } return ints; } /// /// 取得自定义长度的字符串 /// /// 字符串 /// 长度 /// 截取后字符串 public static string CutChar(this string str, int lenght) { if (str.Length > lenght) { return str.Substring(0, lenght) + DString.OMITTED; } else { return str; } } /// /// 取得自定义长度的字符串 中文123456789 /// /// 字符串 /// 长度 /// 截取后字符串 public static string CutCNChar(this string str, int lenght) { string ret = String.Empty; if (str.Length > lenght) { int tempnum1 = 0; int tempnum2 = 0; byte[] byitem = System.Text.ASCIIEncoding.ASCII.GetBytes(str); for (int i = 0; i < str.Length; i++) { if ((int)byitem[i] != 63) { tempnum1++; } else { tempnum2++; } if (tempnum2 * 2 + tempnum1 >= lenght * 2) { break; } } ret = str.Substring(0, tempnum2 + tempnum1) + DString.OMITTED; } else { ret = str; } return ret; } /// /// 返回字符串真实长度 1个汉字长度为2 /// /// 字符串 /// 实际字符串长度 public static int GetCharLength(this string str) { return Encoding.Default.GetBytes(str).Length; } /// /// 检测是否有Sql危险字符 /// /// 要判断字符串 /// 判断结果 public static bool IsSafeSqlString(this string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } public static bool IsSafety(this string str) { string text1 = Regex.Replace(str.Replace("%20", " "), @"\s", " "); string text2 = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or "; return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase); } /// /// 替换字符串 /// /// 要替换的字符型 /// 替换后的字符串 public static string ReplaceW(this string str) { return Regex.Replace(str, DString.W, String.Empty).ToString(); } /// /// SHA1加密字符串 /// /// 要加密的字符串 /// 加密后的字符串 public static string EncryptSHA1(this string str) { if (!str.IsEmpty()) { return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.SHA1); } return str; } /// /// MD5加密字符串 /// /// 要加密的字符串 /// 加密后的字符串 public static string EncryptMD5(this string str) { if (!str.IsEmpty()) { return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.MD5); } return str; } /// /// DES加密字符串 /// /// 待加密的字符串 /// 加密密钥,要求为8位 /// 加密成功返回加密后的字符串,失败返回源串 public static string EncryptDES(this string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = DConfig.KEYS; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch (Exception ex) { // Log.WriteLog(DMessage.ERR, ex); } return encryptString; } /// /// DES解密字符串 /// /// 待解密的字符串 /// 解密密钥,要求为8位,和加密密钥相同 /// 解密成功返回解密后的字符串,失败返源串 public static string DecryptDES(this string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = DConfig.KEYS; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch (Exception ex) { ////DLog.WriteLog(DMessage.ERR, ex); } return decryptString; } /// /// 生成随机字符串 /// /// 字符串长度 /// 随机字符串 public static string RandChar(int length) { string strSep = DString.COMMA; char[] chrSep = strSep.ToCharArray(); //因1与l不容易分清楚,所以去除 //因0与O不容易分清楚,所以去除 string strChar = DString.CHAR; string[] aryChar = strChar.Split(chrSep, strChar.Length); string strRandom = string.Empty; Random Rnd = new Random(); //生成随机字符串 for (int i = 0; i < length; i++) { strRandom += aryChar[Rnd.Next(aryChar.Length)]; } return strRandom; } /// /// 生成随机数字字符串 /// /// 字符串长度 /// 随机字符串 public static string RandNum(int length) { string strSep = DString.COMMA; char[] chrSep = strSep.ToCharArray(); //因1与l不容易分清楚,所以去除 //因0与O不容易分清楚,所以去除 string strChar = DString.NUM; string[] aryChar = strChar.Split(chrSep, strChar.Length); string strRandom = string.Empty; Random rnd = new Random(); //生成随机字符串 for (int i = 0; i < length; i++) { strRandom += aryChar[rnd.Next(aryChar.Length)]; } return strRandom; } /// /// 生成随机汉子字符串 /// /// 字符串长度 /// 随机字符串 public static string RandCN(int length) { string strSep = DString.COMMA; char[] chrSep = strSep.ToCharArray(); string strChar = DString.CN; //定义一个字符串数组储存汉字编码的组成元素 string[] rBase = strChar.Split(chrSep, strChar.Length); Random rnd = new Random(); //定义一个object数组用来 object[] bytes = new object[length]; /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中 每个汉字有四个区位码组成 区位码第1位和区位码第2位作为字节数组第一个元素 区位码第3位和区位码第4位作为字节数组第二个元素 */ for (int i = 0; i < length; i++) { //区位码第1位 int r1 = rnd.Next(11, 14); string str_r1 = rBase[r1].Trim(); //区位码第2位 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值 int r2; if (r1 == 13) { r2 = rnd.Next(0, 7); } else { r2 = rnd.Next(0, 16); } string str_r2 = rBase[r2].Trim(); //区位码第3位 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i); int r3 = rnd.Next(10, 16); string str_r3 = rBase[r3].Trim(); //区位码第4位 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i); int r4; if (r3 == 10) { r4 = rnd.Next(1, 16); } else if (r3 == 15) { r4 = rnd.Next(0, 15); } else { r4 = rnd.Next(0, 16); } string str_r4 = rBase[r4].Trim(); //定义两个字节变量存储产生的随机汉字区位码 byte byte1 = Convert.ToByte(str_r1 + str_r2, 16); byte byte2 = Convert.ToByte(str_r3 + str_r4, 16); //将两个字节变量存储在字节数组中 byte[] str_r = new byte[] { byte1, byte2 }; //将产生的一个汉字的字节数组放入object数组中 bytes.SetValue(str_r, i); } Encoding gb = Encoding.GetEncoding(DString.GBK); string[] str = new string[length]; string chars = String.Empty; for (int i = 0; i < length; i++) { str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); chars = chars + str[i]; } return chars; } /// /// 生成随机文件名 /// /// 文件名 /// 随机文件名 public static string RandFileName() { return DObject.DATE_TIME + RandChar(5); } /// /// 生成随机订单号 /// /// 随机文件名 public static string RandOrder() { return DObject.DATE_TIME + RandNum(8); } /// /// 生成小文件名 /// /// 原文件名 public static string GetMin(this string filename) { return filename.Substring(0, filename.LastIndexOf(DString.POINT)) + DString.MIN + filename.Substring(filename.LastIndexOf(DString.POINT), filename.Length - filename.LastIndexOf(DString.POINT)); } /// /// 生成缩略图 /// /// 源图路径(物理路径) /// 缩略图路径(物理路径) /// 缩略图宽度 /// 缩略图高度 /// 生成缩略图的方式 3:指定高宽缩放(可能变形)2:指定宽,高按比例 1:指定高,宽按比例0:指定高宽裁减(不变形) public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, int mode) { bool isSuccess = true; Image originalImage = Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case 3://指定高宽缩放(可能变形) break; case 2://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case 1://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case 0://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 Image bitmap = new Bitmap(towidth, toheight); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.White); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, ImageFormat.Jpeg); } catch (System.Exception ex) { isSuccess = false; ////DLog.WriteLog(DMessage.ERR, ex); } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } return isSuccess; } /// /// 根据属性名提取属性值 /// /// 类型 /// 要提取的对象 /// 属性名 /// 提取出的属性值 public static object GetPropertyByName(T obj, string name) { try { Type t = obj.GetType(); PropertyInfo pi = t.GetProperty(name); return pi.GetValue(obj, null); } catch (System.Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return new Object(); } /// /// 根据属性名设置值 /// /// 类型 /// 要设置的对象 /// 属性名 /// 属性值 /// 设置值后的对象 public static T SetPropertyByName(T obj, string name, object val) { try { Type t = obj.GetType(); PropertyInfo pi = t.GetProperty(name); pi.SetValue(obj, val, null); } catch (System.Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return obj; } #endregion #region 网页 #region 常用 /// /// 提取网址 /// /// 地址 /// 真实地址 public static string GetResolvedUrl(this string url) { return DObject.CURRECT_PAGE.ResolveUrl(url); } #endregion #region 页面跳转 /// /// 页面出错 /// public static void GoError() { DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_ERROR); } /// /// 无访问权限 /// public static void GoNoPower() { DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_NOACCESS); } /// /// 找不到页面 /// public static void GoNoFound() { DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_NOFOUND); } /// /// 网站维护中 /// public static void GoMaintenance() { DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_MAINTENANCE); } #endregion #region COOKIE /// /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires /// /// COOKIE对象名 /// COOKIE对象有效时间(秒数),0一周内有效,1表示永久有效,大于等于2表示具体有效秒数 /// COOKIE对象Value值 public static void SetCookie(string strCookieName, int iExpires, string strValue) { HttpCookie objCookie = new HttpCookie(strCookieName); objCookie.Value = UrlEncode(strValue.Trim()); if (iExpires >= 0) { if (iExpires.Equals(0)) { objCookie.Expires = DateTime.Now.AddDays(7); } else if (iExpires.Equals(1)) { objCookie.Expires = DateTime.MaxValue; } else { objCookie.Expires = DateTime.Now.AddSeconds(iExpires); } } DObject.CURRECT_PAGE.Response.Cookies.Add(objCookie); } /// /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回字符串"CookieNonexistence" /// /// Cookie对象名称 /// Value值,如果对象本就不存在,则返回字符串"" public static string GetCookie(string strCookieName) { if (DObject.CURRECT_PAGE.Request.Cookies[strCookieName].IsNull()) { return String.Empty; } else { return UrlDecode(DObject.CURRECT_PAGE.Request.Cookies[strCookieName].Value); } } /// /// URL解码 /// /// 要解码的字符串 /// 解码后的字符串 public static string UrlDecode(this string str) { try { return HttpUtility.UrlDecode(str); } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return str; } /// /// URL编码 /// /// 要编码的字符串 /// 编码后的字符串 public static string UrlEncode(this string str) { try { return HttpUtility.UrlEncode(str); } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return str; } /// /// HTML解码 /// /// 要解码的字符串 /// 解码后的字符串 public static string HtmlDecode(this string str) { try { return HttpUtility.HtmlDecode(str); } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return str; } /// /// HTML编码 /// /// 要编码的字符串 /// 编码后的字符串 public static string HtmlEncode(this string str) { try { return HttpUtility.HtmlEncode(str); } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return str; } #endregion #region Session /// /// 读取session值 /// /// 名称 /// public static object GetSession(string name) { object Str_Value = null; try { Str_Value = DObject.CURRECT_PAGE.Session[name]; } catch (Exception ex) { Str_Value = null; //DLog.WriteLog(DMessage.ERR, ex); } return Str_Value; } /// /// 读取session值 /// /// 名称 /// public static string GetStrSession(string name) { string Str_Value = null; try { Str_Value = DObject.CURRECT_PAGE.Session[name].ToString(); } catch (Exception ex) { Str_Value = String.Empty; //DLog.WriteLog(DMessage.ERR, ex); } return Str_Value; } /// /// 设置session值 /// /// 名称 /// 值 public static void SetSession(string name, object value) { try { DObject.CURRECT_PAGE.Session[name] = value; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } } #endregion #region HTML /// /// 读取提交值 /// /// 名称 /// 提交值 public static string GetQueryString(string name) { if (DObject.CURRECT_PAGE.Request[name].IsNull()) { return String.Empty; } return DObject.CURRECT_PAGE.Request[name]; } /// /// 取得当前URL /// /// 当前URL public static string GetPath() { string strPath = String.Format(DString.STRING_URL, HttpContext.Current.Request.ServerVariables[DString.STRING_HOST], HttpContext.Current.Request.ServerVariables[DString.STRING_PATH_INFO], HttpContext.Current.Request.ServerVariables[DString.STRING_QUERY_STRING]); if (strPath.IndexOf(DString.QM) > 0) { strPath = strPath.Substring(0, strPath.IndexOf(DString.QM)); } return strPath; } /// /// Aspx 转换到 Html /// /// 地址 /// 字符串 public static string AspxToHtml(this string url) { //判断路径是否为空 if (!url.IsEmpty()) { //分割路径 string[] temp = url.Split(DString.CHAR_QM); //路径不合法 if (temp.Length != 1 && temp.Length != 2) { return url; } //获取路径后缀 string ext = Path.GetExtension(temp[0]); //后缀不为ASPX直接返回 if (!(ext.Equals(DString.STRING_ASPX, StringComparison.OrdinalIgnoreCase))) { return url; } //截取.aspx中前面的内容 int offset = temp[0].LastIndexOf(DString.CHAR_POINT); string resource = temp[0].Substring(0, offset); //路径不带参数时 if (temp.Length == 1 || string.IsNullOrEmpty(temp[1])) { return string.Format(DString.STRING_HTML0, resource); } string aurl = temp[1].Replace(DString.CHAR_EQ, DString.CHAR_UNDERLINE); //路径带参数时 return string.Format(DString.STRING_HTML01, resource, aurl.Replace(DString.AMP, DString.UNDERLINE_T)); } return String.Empty; } /// /// Html 转换到 Aspx /// /// 地址 /// 字符串 public static string HtmlToAspx(this string url) { //判断路径是否为空 if (!url.IsEmpty()) { string ext = Path.GetExtension(url); //路径不为HTML if (!(ext.Equals(DString.STRING_HTML, StringComparison.OrdinalIgnoreCase))) { return url; } string[] temp = url.Split(new String[] { DString.UNDERLINE_TH, DString.POINT }, StringSplitOptions.RemoveEmptyEntries); if (temp.Length == 2) { return string.Format(DString.STRING_ASPX0, temp[0]); } if (temp.Length == 3) { string aurl = temp[1].Replace(DString.UNDERLINE_T, DString.AMP); return String.Format(DString.STRING_ASPX01, temp[0], aurl.Replace(DString.CHAR_UNDERLINE, DString.CHAR_EQ)); } } return String.Empty; } /// /// 过滤HTML代码 /// /// 要过滤的数据 /// 过滤后的数据 public static string FilterHTML(this string str) { Regex regexScript = new Regex(DString.STRING_SCRIPT, RegexOptions.IgnoreCase); Regex regexHref = new Regex(DString.STRING_HREFSCRIPT, RegexOptions.IgnoreCase); Regex regexOn = new Regex(DString.HTML_EVEN, RegexOptions.IgnoreCase); Regex regexIframe = new Regex(DString.STRING_IFRAME, RegexOptions.IgnoreCase); Regex regexFrameset = new Regex(DString.HTML_IFRAMESET, RegexOptions.IgnoreCase); string html = str.Trim(); html = regexScript.Replace(html, String.Empty); //过滤标记 html = regexHref.Replace(html, String.Empty); //过滤href=javascript: () 属性 html = regexOn.Replace(html, DString.HTML_EVEN_R); //过滤其它控件的on事件 html = regexIframe.Replace(html, String.Empty); //过滤iframe html = regexFrameset.Replace(html, String.Empty); //过滤frameset return html; } #endregion #endregion #region 类型转换 /// /// 转换成INT /// /// 要转换的对象 /// 转换结果 public static int ToInt32(this object obj) { try { if (obj.IsNull()) { return 0; } int ret = Convert.ToInt32(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return 0; } /// /// 转换成DataTable /// /// /// public static DataTable ToDataTable(this object obj) { try { if (obj.IsNull()) { return new DataTable(); } DataTable ret = obj as DataTable; return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return new DataTable(); } /// /// 转换成List集合 /// /// /// /// public static List ToModList(this object obj) where T : new() { try { if (obj.IsNull()) { return new List(); } List ret = obj as List; return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return new List(); } /// /// 转换成List集合 /// /// /// /// public static T ToMod(this object obj) where T : new() { try { if (obj.IsNull()) { return new T(); } T ret = (T)obj; return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return new T(); } /// /// 转换成INT /// /// 要转换的对象 /// 转换结果 public static int ToInt32(this object obj, int defValue) { try { if (obj.IsNull()) { return defValue; } int ret = Convert.ToInt32(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return defValue; } /// /// 转换成INT /// /// 要转换的对象 /// 转换结果 public static decimal ToDec(this object obj) { try { if (obj.IsNull()) { return 0; } decimal ret = Convert.ToDecimal(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return 0; } /// /// 转换成INT /// /// 要转换的对象 /// 转换结果 public static decimal ToDec(this object obj, decimal defValue) { try { if (obj.IsNull()) { return defValue; } decimal ret = Convert.ToDecimal(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return defValue; } /// /// 转换成BOOL /// /// 要转换的对象 /// 转换结果 public static double ToDou(this object obj) { try { if (obj.IsNull()) { return 0; } double ret = Convert.ToDouble(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return 0; } public static float ToFloat(this object obj, int defValue) { try { if (obj.IsNull()) { return defValue; } float ret = Convert.ToInt32(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return defValue; } /// /// 转换成BOOL /// /// 要转换的对象 /// 转换结果 public static double ToDou(this object obj, double defValue) { try { if (obj.IsNull()) { return defValue; } double ret = Convert.ToDouble(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return defValue; } /// /// 转换成BOOL /// /// 要转换的对象 /// 转换结果 public static bool ToBool(this object obj) { try { if (obj.IsNull()) { return false; } bool ret = Convert.ToBoolean(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return false; } /// /// 转换成BOOL /// /// 要转换的对象 /// 转换结果 public static DateTime ToDateTime(this object obj) { try { DateTime ret = Convert.ToDateTime(obj); return ret; } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return DObject.EMPTY_DATE_TIME; } public static string ToDateTimeStr(this DateTime obj) { try { return obj.ToString("yyyy-MM-dd"); } catch (Exception ex) { //DLog.WriteLog(DMessage.ERR, ex); } return ""; } #endregion }