123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Common.Helper
- {
- public static class UtilConvert
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static int ObjToInt(this object thisValue)
- {
- int reval = 0;
- if (thisValue == null) return 0;
- if (thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return reval;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <param name="errorValue"></param>
- /// <returns></returns>
- public static int ObjToInt(this object thisValue, int errorValue)
- {
- int reval = 0;
- if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return errorValue;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static double ObjToMoney(this object thisValue)
- {
- double reval = 0;
- if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return 0;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <param name="errorValue"></param>
- /// <returns></returns>
- public static double ObjToMoney(this object thisValue, double errorValue)
- {
- double reval = 0;
- if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return errorValue;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static string ObjToString(this object thisValue)
- {
- if (thisValue != null) return thisValue.ToString().Trim();
- return "";
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static bool IsNotEmptyOrNull(this object thisValue)
- {
- return ObjToString(thisValue) != "" && ObjToString(thisValue) != "undefined" && ObjToString(thisValue) != "null";
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <param name="errorValue"></param>
- /// <returns></returns>
- public static string ObjToString(this object thisValue, string errorValue)
- {
- if (thisValue != null) return thisValue.ToString().Trim();
- return errorValue;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static Decimal ObjToDecimal(this object thisValue)
- {
- Decimal reval = 0;
- if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return 0;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <param name="errorValue"></param>
- /// <returns></returns>
- public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
- {
- Decimal reval = 0;
- if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return errorValue;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static DateTime ObjToDate(this object thisValue)
- {
- DateTime reval = DateTime.MinValue;
- if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
- {
- reval = Convert.ToDateTime(thisValue);
- }
- return reval;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <param name="errorValue"></param>
- /// <returns></returns>
- public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
- {
- DateTime reval = DateTime.MinValue;
- if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return errorValue;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static bool ObjToBool(this object thisValue)
- {
- bool reval = false;
- if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
- {
- return reval;
- }
- return reval;
- }
- /// <summary>
- /// 获取当前时间的时间戳
- /// </summary>
- /// <param name="thisValue"></param>
- /// <returns></returns>
- public static string DateToTimeStamp(this DateTime thisValue)
- {
- TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- return Convert.ToInt64(ts.TotalSeconds).ToString();
- }
- }
- }
|