記錄下在開發(fā)中經(jīng)常用到的類型轉(zhuǎn)換文章來源:http://www.zghlxwxcb.cn/news/detail-711559.html
#region 轉(zhuǎn)換為string /// <summary> /// 將object轉(zhuǎn)換為string,若轉(zhuǎn)換失敗,則返回""。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ParseToString(this object obj) { return Convert.ToString(obj) ?? string.Empty; } #endregion #region 轉(zhuǎn)換為long /// <summary> /// 將object轉(zhuǎn)換為long,若轉(zhuǎn)換失敗,則返回0。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static long ParseToLong(this object obj) { if (obj == null) { return 0; } else { bool boolean = long.TryParse(obj.ToString() ?? string.Empty, out long result); return result; } } /// <summary> /// 將object轉(zhuǎn)換為long,若轉(zhuǎn)換失敗,則返回指定值。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static long ParseToLong(this object obj, long defaultValue) { if (obj == null) { return defaultValue; } else { bool boolean = long.TryParse(obj.ToString() ?? string.Empty, out long result); if (boolean) return result; else return defaultValue; } } #endregion #region 轉(zhuǎn)換為int /// <summary> /// 將object轉(zhuǎn)換為int,若轉(zhuǎn)換失敗,則返回0。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static int ParseToInt(this object obj) { if (obj == null) { return 0; } else { bool boolean = int.TryParse(obj.ToString() ?? string.Empty, out int result); return result; } } /// <summary> /// 將object轉(zhuǎn)換為int,若轉(zhuǎn)換失敗,則返回指定值。不拋出異常。 /// null返回默認值 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static int ParseToInt(this object obj, int defaultValue) { if (obj == null) { return defaultValue; } else { bool boolean = int.TryParse(obj.ToString() ?? string.Empty, out int result); if (boolean) return result; else return defaultValue; } } #endregion #region 轉(zhuǎn)換為double /// <summary> /// 將object轉(zhuǎn)換為double,若轉(zhuǎn)換失敗,則返回0。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static double ParseToDouble(this object obj) { if (obj == null) { return 0; } else { bool boolean = double.TryParse(obj.ToString() ?? string.Empty, out double result); return result; } } /// <summary> /// 將object轉(zhuǎn)換為double,若轉(zhuǎn)換失敗,則返回指定值。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static double ParseToDouble(this object obj, double defaultValue) { if (obj == null) { return defaultValue; } else { bool boolean = double.TryParse(obj.ToString() ?? string.Empty, out double result); if (boolean) return result; else return defaultValue; } } #endregion #region 轉(zhuǎn)換為demical /// <summary> /// 將object轉(zhuǎn)換為demical,若轉(zhuǎn)換失敗,則返回0。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static decimal ParseToDecimal(this object obj) { if (obj == null) { return 0; } else { bool boolean = decimal.TryParse(obj.ToString() ?? string.Empty, out decimal result); return result; } } /// <summary> /// 將object轉(zhuǎn)換為demical,若轉(zhuǎn)換失敗,則返回指定值。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static decimal ParseToDecimal(this object obj, decimal defaultValue) { if (obj == null) { return defaultValue; } else { bool boolean = decimal.TryParse(obj.ToString() ?? string.Empty, out decimal result); if (boolean) return result; else return defaultValue; } } #endregion #region 轉(zhuǎn)化為bool /// <summary> /// 將object轉(zhuǎn)換為bool,若轉(zhuǎn)換失敗,則返回false。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool ParseToBool(this object obj) { if (obj == null) { return false; } else { bool boolean = bool.TryParse(obj.ToString() ?? string.Empty, out bool result); return result; } } /// <summary> /// 將object轉(zhuǎn)換為bool,若轉(zhuǎn)換失敗,則返回指定值。不拋出異常。 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static bool ParseToBool(this object obj, bool defaultValue) { if (obj == null) { return defaultValue; } else { bool boolean = bool.TryParse(obj.ToString() ?? string.Empty, out bool result); if (boolean) return result; else return defaultValue; } } #endregion #region 時間與時間戳轉(zhuǎn)換 /// <summary> /// 時間轉(zhuǎn)換為毫秒時間戳 /// </summary> /// <param name="nowTime"></param> /// <returns>時間戳(毫秒)</returns> public static long ParseToUnixTime(this DateTime nowTime) { var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); return (long)Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero); } #endregion #region 刪除最后一個字符之后的字符 /// <summary> /// 刪除最后結(jié)尾的一個逗號 /// </summary> public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); } /// <summary> /// 刪除最后結(jié)尾的指定字符后的字符 /// </summary> public static string DelLastChar(string str, string strchar) { return str.Substring(0, str.LastIndexOf(strchar)); } /// <summary> /// 刪除最后結(jié)尾的長度 /// </summary> /// <param name="str"></param> /// <param name="Length"></param> /// <returns></returns> public static string DelLastLength(string str, int Length) { if (string.IsNullOrEmpty(str)) return ""; str = str.Substring(0, str.Length - Length); return str; } #endregion #region 強制轉(zhuǎn)換類型 /// <summary> /// 強制轉(zhuǎn)換類型 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="source"></param> /// <returns></returns> public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source) { return from object item in source select (TResult)Convert.ChangeType(item, typeof(TResult)); } #endregion #region List轉(zhuǎn)DataTable /// <summary> /// List轉(zhuǎn)DataTable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static DataTable ListToDataTable<T>(IEnumerable<T> list) { //創(chuàng)建屬性的集合 List<PropertyInfo> pList = new List<PropertyInfo>(); //獲得反射的入口 Type type = typeof(T); DataTable dt = new DataTable(); //把所有的public屬性加入到集合 并添加DataTable的列 Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); var theType = p.PropertyType; //處理可空類型 if (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { dt.Columns.Add(p.Name, Nullable.GetUnderlyingType(theType)); } else { dt.Columns.Add(p.Name, theType); } }); foreach (var item in list) { //創(chuàng)建一個DataRow實例 DataRow row = dt.NewRow(); //給row 賦值 pList.ForEach(p => { var v = p.GetValue(item, null); row[p.Name] = v == null ? DBNull.Value : v; }); //加入到DataTable dt.Rows.Add(row); } return dt; } /// <summary> /// List轉(zhuǎn)DataTable /// </summary> /// <param name="list"></param> /// <returns></returns> public static DataTable ListToDataTableNew(List<dynamic> list) { Dictionary<string, string> dic = new Dictionary<string, string>(); DataTable dt = new DataTable(); if (list != null && list.Count > 0) { foreach (var item in list[0]) { string text = item.ToString(); string key = text.Substring(1, text.IndexOf(',') - 1); string value = text.Substring(text.IndexOf(',') + 1, text.Length - text.IndexOf(',') - 2); dt.Columns.Add(key); } } foreach (var vlist in list) { DataRow row = dt.NewRow(); foreach (var item in vlist) { string text = item.ToString(); string key = text.Substring(1, text.IndexOf(',') - 1); string value = text.Substring(text.IndexOf(',') + 1, text.Length - text.IndexOf(',') - 2); //創(chuàng)建一個DataRow實例 row[key] = value; } //加入到DataTable dt.Rows.Add(row); } return dt; } #endregion
?文章來源地址http://www.zghlxwxcb.cn/news/detail-711559.html
到了這里,關(guān)于.NET 一些常用的類型轉(zhuǎn)換擴展的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!