通用扩展函数--类型转换
生活随笔
收集整理的這篇文章主要介紹了
通用扩展函数--类型转换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
調(diào)用:
//轉(zhuǎn)intvar int1 = "2".TryToInt();//轉(zhuǎn)換為int失敗返回0var int2 = "2x".TryToInt();var int3 = "2".TryToInt(1);//轉(zhuǎn)換為int失敗返回1var int4 = "2x".TryToInt(1);//轉(zhuǎn)doublevar d1 = "2".TryToMoney(); //同上var d2 = "2x".TryToMoney();var d3 = "2".TryToMoney(1);var d4 = "2x".TryToMoney(1);//轉(zhuǎn)stringstring a = null;var s1 = a.TryToString();var s3 = a.TryToString("1");//轉(zhuǎn)decimalvar d11 = "2".TryToDecimal();var d22 = "2x".TryToDecimal();var d33 = "2".TryToDecimal(1);var d44 = "2x".TryToDecimal(1);//轉(zhuǎn)datetimevar 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<Model>();//list和dataTable轉(zhuǎn)換var dt = new List<Model>() { new Model() { id = 1 } }.ListToDataTable();var list = dt.DataTableToList<Model>();代碼:(DataTable處做了修改)
/// <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序列化為實體/// <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 將集合類list轉(zhuǎn)換成DataTable/// <summary>/// 將集合類list轉(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);tempList.Add(obj);}object[] array = tempList.ToArray();result.LoadDataRow(array, true);}}return result;}#endregion#region 將DataTable轉(zhuǎn)為集合類list/// <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}?
?
文章出自:http://www.cnblogs.com/sunkaixuan/p/4548028.html
轉(zhuǎn)載于:https://www.cnblogs.com/cang12138/p/5774064.html
總結(jié)
以上是生活随笔為你收集整理的通用扩展函数--类型转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。