国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NETC#通用擴展函數之TypeParse 類型轉換方便多了

2019-11-17 02:08:35
字體:
來源:轉載
供稿:網友

asp.netC#通用擴展函數之TypeParse 類型轉換方便多了

用法:

            var int1 = "2".TryToInt();//轉換為int失敗返回0            var int2 = "2x".TryToInt();            var int3 = "2".TryToInt(1);//轉換為int失敗返回1            var int4 = "2x".TryToInt(1);            var d1 = "2".TryToMoney(); //同上            var d2 = "2x".TryToMoney();            var d3 = "2".TryToMoney(1);            var d4 = "2x".TryToMoney(1);            string a = null;            var s1 = a.TryToString();            var s3 = a.TryToString("1");            var d11 = "2".TryToDecimal();            var d22 = "2x".TryToDecimal();            var d33 = "2".TryToDecimal(1);            var d44 = "2x".TryToDecimal(1);            var de1 = "2013-1-1".TryToDate();            var de2 = "x2013-1-1".TryToDate();            var de3 = "x2013-1-1".TryToDate(DateTime.Now);            //json和model轉換            var json = new { id = 1 }.ModelToJson();            var model = "{id:1}".JsonToModel<ModelTest>();            //list和dataTable轉換            var dt = new List<ModelTest>().ListToDataTable();            var list = dt.DataTableToList<ModelTest>();

  

代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Script.Serialization;using System.Data;using System.Reflection;using System.Collections;namespace SyntacticSugar{    /// <summary>    /// ** 描述:類型轉換    /// ** 創始時間:2015-6-2    /// ** 修改時間:-    /// ** 作者:sunkaixuan    /// ** 使用說明:    /// </summary>    public static class TypeParseExtenions    {        #region 強轉成int 如果失敗返回 0        /// <summary>        /// 強轉成int 如果失敗返回 0        /// </summary>        /// <param name="thisValue"></param>        /// <param name="i"></param>        /// <returns></returns>        public static int TryToInt(this object thisValue)        {            int reval = 0;            if (thisValue != null && int.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return reval;        }        #endregion        #region 強轉成int 如果失敗返回 errorValue        /// <summary>        /// 強轉成int 如果失敗返回 errorValue        /// </summary>        /// <param name="thisValue"></param>        /// <param name="i"></param>        /// <returns></returns>        public static int TryToInt(this object thisValue, int errorValue)        {            int reval = 0;            if (thisValue != null && int.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return errorValue;        }        #endregion        #region 強轉成double 如果失敗返回 0        /// <summary>        /// 強轉成money 如果失敗返回 0        /// </summary>        /// <param name="thisValue"></param>        /// <param name="i"></param>        /// <returns></returns>        public static double TryToMoney(this object thisValue)        {            double reval = 0;            if (thisValue != null && double.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return 0;        }        #endregion        #region 強轉成double 如果失敗返回 errorValue        /// <summary>        /// 強轉成double 如果失敗返回 errorValue        /// </summary>        /// <param name="thisValue"></param>        /// <param name="errorValue"></param>        /// <returns></returns>        public static double TryToMoney(this object thisValue, int errorValue)        {            double reval = 0;            if (thisValue != null && double.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return errorValue;        }        #endregion        #region 強轉成string 如果失敗返回 ""        /// <summary>        /// 強轉成string 如果失敗返回 ""        /// </summary>        /// <param name="thisValue"></param>        /// <param name="i"></param>        /// <returns></returns>        public static string TryToString(this object thisValue)        {            if (thisValue != null) return thisValue.ToString().Trim();            return "";        }        #endregion        #region  強轉成string 如果失敗返回 errorValue        /// <summary>        /// 強轉成string 如果失敗返回 str        /// </summary>        /// <param name="thisValue"></param>        /// <param name="errorValue"></param>        /// <returns></returns>        public static string TryToString(this object thisValue, string errorValue)        {            if (thisValue != null) return thisValue.ToString().Trim();            return errorValue;        }        #endregion        #region 強轉成Decimal 如果失敗返回 0        /// <summary>        /// 強轉成Decimal 如果失敗返回 0        /// </summary>        /// <param name="thisValue"></param>        /// <param name="i"></param>        /// <returns></returns>        public static Decimal TryToDecimal(this object thisValue)        {            Decimal reval = 0;            if (thisValue != null && decimal.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return 0;        }        #endregion        #region 強轉成Decimal 如果失敗返回 errorValue        /// <summary>        /// 強轉成Decimal 如果失敗返回 errorValue        /// </summary>        /// <param name="thisValue"></param>        /// <param name="errorValue"></param>        /// <returns></returns>        public static Decimal TryToDecimal(this object thisValue, int errorValue)        {            Decimal reval = 0;            if (thisValue != null && decimal.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return errorValue;        }        #endregion        #region 強轉成DateTime 如果失敗返回 DateTime.MinValue        /// <summary>        /// 強轉成DateTime 如果失敗返回 DateTime.MinValue        /// </summary>        /// <param name="thisValue"></param>        /// <param name="i"></param>        /// <returns></returns>        public static DateTime TryToDate(this object thisValue)        {            DateTime reval = DateTime.MinValue;            if (thisValue != null && DateTime.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return reval;        }        #endregion        #region 強轉成DateTime 如果失敗返回 errorValue        /// <summary>        /// 強轉成DateTime 如果失敗返回 errorValue        /// </summary>        /// <param name="thisValue"></param>        /// <param name="errorValue"></param>        /// <returns></returns>        public static DateTime TryToDate(this object thisValue, DateTime errorValue)        {            DateTime reval = DateTime.MinValue;            if (thisValue != null && DateTime.TryParse(thisValue.ToString(), out reval))            {                return reval;            }            return errorValue;        }        #endregion        #region json轉換        /// <summary>        /// 將json序列化為實體        /// </summary>        /// <typeparam name="TEntity"></typeparam>        /// <param name="json"></param>        /// <returns></returns>        public static TEntity JsonToModel<TEntity>(this string json)        {            javaScriptSerializer jsSerializer = new JavascriptSerializer();            return jsSerializer.Deserialize<TEntity>(json);        }        /// <summary>        /// 將實體序列化為json        /// </summary>        /// <param name="model"></param>        /// <returns></returns>        public static string ModelToJson<T>(this T model)        {            JavaScriptSerializer jsSerializer = new JavaScriptSerialize
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旺苍县| 安阳市| 电白县| 新化县| 郯城县| 韶关市| 深州市| 芜湖市| 凯里市| 宝丰县| 延川县| 右玉县| 黄龙县| 体育| 景宁| 图木舒克市| 望城县| 芜湖市| 南华县| 天全县| 开化县| 观塘区| 平山县| 怀集县| 吉首市| 湛江市| 黄龙县| 定安县| 邳州市| 侯马市| 通辽市| 三江| 洪江市| 温泉县| 澄迈县| 天气| 西青区| 林甸县| 石门县| 四会市| 盐亭县|