123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278 |
- 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 常用处理
-
-
-
-
-
- public static bool IsHaveRows(this System.Data.DataTable dt)
- {
- if (dt != null && dt.Rows.Count > 0)
- return true;
- return false;
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- public static bool IsIndexOf(this string strs, string str)
- {
- if (strs.ToLower().IndexOf(str.ToLower()) != -1)
- return true;
- return false;
- }
-
-
-
-
-
- public static bool IsEmpty(this string str)
- {
- if (IsNull(str))
- {
- return true;
- }
- if (str.Equals(String.Empty))
- {
- return true;
- }
- return false;
- }
-
-
-
-
-
- public static string IfNull(this object str)
- {
- if (str == null || Object.Equals(str, null))
- {
- return String.Empty;
- }
- return str.ToString();
- }
-
-
-
-
-
- public static bool IfNotNull(this object str)
- {
- if ((Object.Equals(str, null)) || (Object.Equals(str, DBNull.Value)) || (str == null))
- {
- return false;
- }
- return true;
- }
-
-
-
-
- public static bool IsValuable(this IEnumerable<object> thisValue)
- {
- if (thisValue == null || thisValue.Count() == 0) return false;
- return true;
- }
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- public static int GetCharLength(this string str)
- {
- return Encoding.Default.GetBytes(str).Length;
- }
-
-
-
-
-
- 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();
- }
-
-
-
-
-
- public static string EncryptSHA1(this string str)
- {
- if (!str.IsEmpty())
- {
- return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.SHA1);
- }
- return str;
- }
-
-
-
-
-
- public static string EncryptMD5(this string str)
- {
- if (!str.IsEmpty())
- {
- return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.MD5);
- }
- return str;
- }
-
-
-
-
-
-
- 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)
- {
-
- }
- return encryptString;
- }
-
-
-
-
-
-
- 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)
- {
-
- }
- return decryptString;
- }
-
-
-
-
-
- public static string RandChar(int length)
- {
- string strSep = DString.COMMA;
- char[] chrSep = strSep.ToCharArray();
-
-
- 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();
-
-
- 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[] bytes = new object[length];
-
- for (int i = 0; i < length; i++)
- {
-
- int r1 = rnd.Next(11, 14);
- string str_r1 = rBase[r1].Trim();
-
- 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();
-
- rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
- int r3 = rnd.Next(10, 16);
- string str_r3 = rBase[r3].Trim();
-
- 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 };
-
- 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));
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
- 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
- {
-
- bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
- }
- catch (System.Exception ex)
- {
- isSuccess = false;
-
- }
- finally
- {
- originalImage.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- return isSuccess;
- }
-
-
-
-
-
-
-
- public static object GetPropertyByName<T>(T obj, string name)
- {
- try
- {
- Type t = obj.GetType();
- PropertyInfo pi = t.GetProperty(name);
- return pi.GetValue(obj, null);
- }
- catch (System.Exception ex)
- {
-
- }
- return new Object();
- }
-
-
-
-
-
-
-
-
- public static T SetPropertyByName<T>(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)
- {
-
- }
- 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
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
- 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);
- }
- }
-
-
-
-
-
- public static string UrlDecode(this string str)
- {
- try
- {
- return HttpUtility.UrlDecode(str);
- }
- catch (Exception ex)
- {
-
- }
- return str;
- }
-
-
-
-
-
- public static string UrlEncode(this string str)
- {
- try
- {
- return HttpUtility.UrlEncode(str);
- }
- catch (Exception ex)
- {
-
- }
- return str;
- }
-
-
-
-
-
- public static string HtmlDecode(this string str)
- {
- try
- {
- return HttpUtility.HtmlDecode(str);
- }
- catch (Exception ex)
- {
-
- }
- return str;
- }
-
-
-
-
-
- public static string HtmlEncode(this string str)
- {
- try
- {
- return HttpUtility.HtmlEncode(str);
- }
- catch (Exception ex)
- {
-
- }
- return str;
- }
- #endregion
- #region 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;
-
- }
- return Str_Value;
- }
-
-
-
-
-
- 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;
-
- }
- return Str_Value;
- }
-
-
-
-
-
- public static void SetSession(string name, object value)
- {
- try
- {
- DObject.CURRECT_PAGE.Session[name] = value;
- }
- catch (Exception 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];
- }
-
-
-
-
- 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;
- }
-
-
-
-
-
- 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]);
-
- if (!(ext.Equals(DString.STRING_ASPX, StringComparison.OrdinalIgnoreCase)))
- {
- return url;
- }
-
- 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;
- }
-
-
-
-
-
- public static string HtmlToAspx(this string url)
- {
-
- if (!url.IsEmpty())
- {
- string ext = Path.GetExtension(url);
-
- 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;
- }
-
-
-
-
-
- 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);
- html = regexOn.Replace(html, DString.HTML_EVEN_R);
- html = regexIframe.Replace(html, String.Empty);
- html = regexFrameset.Replace(html, String.Empty);
- return html;
- }
- #endregion
- #endregion
- #region 类型转换
-
-
-
-
-
- public static int ToInt32(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return 0;
- }
- int ret = Convert.ToInt32(obj);
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return 0;
- }
-
-
-
-
-
- public static DataTable ToDataTable(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return new DataTable();
- }
- DataTable ret = obj as DataTable;
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return new DataTable();
- }
-
-
-
-
-
-
- public static List<T> ToModList<T>(this object obj) where T : new()
- {
- try
- {
- if (obj.IsNull())
- {
- return new List<T>();
- }
- List<T> ret = obj as List<T>;
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return new List<T>();
- }
-
-
-
-
-
-
- public static T ToMod<T>(this object obj) where T : new()
- {
- try
- {
- if (obj.IsNull())
- {
- return new T();
- }
- T ret = (T)obj;
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return new T();
- }
-
-
-
-
-
- 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)
- {
-
- }
- return defValue;
- }
-
-
-
-
-
- public static decimal ToDec(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return 0;
- }
- decimal ret = Convert.ToDecimal(obj);
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return 0;
- }
-
-
-
-
-
- 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)
- {
-
- }
- return defValue;
- }
-
-
-
-
-
- public static double ToDou(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return 0;
- }
- double ret = Convert.ToDouble(obj);
- return ret;
- }
- catch (Exception 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)
- {
-
- }
- return defValue;
- }
-
-
-
-
-
- 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)
- {
-
- }
- return defValue;
- }
-
-
-
-
-
- public static bool ToBool(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return false;
- }
- bool ret = Convert.ToBoolean(obj);
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return false;
- }
-
-
-
-
-
- public static DateTime ToDateTime(this object obj)
- {
- try
- {
- DateTime ret = Convert.ToDateTime(obj);
- return ret;
- }
- catch (Exception ex)
- {
-
- }
- return DObject.EMPTY_DATE_TIME;
- }
- public static string ToDateTimeStr(this DateTime obj)
- {
- try
- {
- return obj.ToString("yyyy-MM-dd");
- }
- catch (Exception ex)
- {
-
- }
- return "";
- }
- #endregion
- }
|