C#、ASP.NET通用擴展工具類之TypeParse
更新時間:2015年06月08日 10:01:23 投稿:junjie
這篇文章主要介紹了C#、ASP.NET通用擴展工具類之TypeParse,使用了此類,類型轉(zhuǎn)換方便多了,本文直接給出實現(xiàn)代碼和使用方法,需要的朋友可以參考下
用法:
var int1 = "2".TryToInt();//轉(zhuǎn)換為int失敗返回0 var int2 = "2x".TryToInt(); var int3 = "2".TryToInt(1);//轉(zhuǎn)換為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轉(zhuǎn)換 var json = new { id = 1 }.ModelToJson(); var model = "{id:1}".JsonToModel<ModelTest>(); //list和dataTable轉(zhuǎn)換 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> /// ** 描述:類型轉(zhuǎn)換 /// ** 創(chuàng)始時間:2015-6-2 /// ** 修改時間:- /// ** 作者:sunkaixuan /// ** 使用說明: /// </summary> public static class TypeParseExtenions { #region 強轉(zhuǎn)成int 如果失敗返回 0 /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成int 如果失敗返回 errorValue /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成double 如果失敗返回 0 /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成double 如果失敗返回 errorValue /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成string 如果失敗返回 "" /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成string 如果失敗返回 errorValue /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成Decimal 如果失敗返回 0 /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成Decimal 如果失敗返回 errorValue /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成DateTime 如果失敗返回 DateTime.MinValue /// <summary> /// 強轉(zhuǎn)成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 強轉(zhuǎn)成DateTime 如果失敗返回 errorValue /// <summary> /// 強轉(zhuǎn)成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轉(zhuǎn)換 /// <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 JavaScriptSerializer(); return jsSerializer.Serialize(model); } #endregion #region DataTable List /// <summary> /// 將集合類轉(zhuǎn)換成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable ListToDataTable<T>(this List<T> list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = typeof(T).GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); if (obj != null && obj != DBNull.Value) tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } /// <summary> /// 將datatable轉(zhuǎn)為list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <returns></returns> public static List<T> DataTableToList<T>(this DataTable dt) { var list = new List<T>(); Type t = typeof(T); var plist = new List<PropertyInfo>(typeof(T).GetProperties()); foreach (DataRow item in dt.Rows) { T s = System.Activator.CreateInstance<T>(); for (int i = 0; i < dt.Columns.Count; i++) { PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName); if (info != null) { if (!Convert.IsDBNull(item[i])) { info.SetValue(s, item[i], null); } } } list.Add(s); } return list; } #endregion } }
您可能感興趣的文章:
- 如何批量清理系統(tǒng)臨時文件(語言:C#、 C/C++、 php 、python 、java )
- C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法
- C#、ASP.NET通用工具類IsWhat?(可以判斷數(shù)字、身份證、數(shù)據(jù)類型等等)
- java、php、C#、asp實現(xiàn)短信群發(fā)功能的方法
- 如何讓C#、VB.NET實現(xiàn)復(fù)雜的二進制操作
- C# Split分隔字符串的應(yīng)用(C#、split、分隔、字符串)
- C#如何安全、高效地玩轉(zhuǎn)任何種類的內(nèi)存之Span的本質(zhì)
相關(guān)文章
C#實現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實例
這篇文章主要介紹了C#實現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù),結(jié)合簡單實例形式分析了C#字符串的轉(zhuǎn)換、遍歷、加密等操作技巧,需要的朋友可以參考下2016-07-07C#讀取QQ純真IP數(shù)據(jù)庫QQWry.Dat的代碼
QQ純真IP庫算是IP地址收集較為全的一個IP庫,對于IP查詢來說這個是不錯的選擇。下面是如何查詢純真IP庫的一個類,使用C#代碼。2007-03-03C#控制Excel Sheet使其自適應(yīng)頁寬與列寬的方法
這篇文章主要介紹了C#控制Excel Sheet使其自適應(yīng)頁寬與列寬的方法,涉及C#操作Excel的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06Unity中的靜態(tài)批處理和動態(tài)批處理操作
這篇文章主要介紹了Unity中的靜態(tài)批處理和動態(tài)批處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#實現(xiàn)AddRange為數(shù)組添加多個元素的方法
這篇文章主要介紹了C#實現(xiàn)AddRange為數(shù)組添加多個元素的方法,實例分析了AddRange方法的使用技巧,需要的朋友可以參考下2015-06-06C#中LinkedList<T>的存儲結(jié)構(gòu)詳解
這篇文章主要介紹了深度解析C#中LinkedList<T>的存儲結(jié)構(gòu),本文將從鏈表的基礎(chǔ)特性、C#中LinkedList的底層實現(xiàn)邏輯,.NET的不同版本對于Queue的不同實現(xiàn)方式的原因分析等幾個視角進行簡單的解讀,需要的朋友可以參考下2023-12-12