using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace ZD.Share.Common { public static class ConvertHelper { public static string JoinToString(this IEnumerable collection, string split) { if (collection == null || !collection.Any()) return string.Empty; StringBuilder sb = new StringBuilder(); foreach (T t in collection) { sb.Append(t).Append(split); } if (sb.Length > 0) { sb.Remove(sb.Length - split.Length, split.Length); } return sb.ToString(); } public static DateTime ToDateTime(this object value, DateTime defaultValue = new DateTime()) { string date = value.ToString(); if (DateTime.TryParse(date, out DateTime temp)) { return temp; } else { return defaultValue; } } public static int ToInt32(this object value, int defaultValue = 0) { string str = value.ToString(); if (int.TryParse(str, out int temp)) { return temp; } else { return defaultValue; } } public static long ToInt64(this object value, long defaultValue = 0) { string str = value.ToString(); if (long.TryParse(str, out long temp)) { return temp; } else { return defaultValue; } } public static decimal ToDecimal(this object value, decimal defaultValue = 0) { string str = value.ToString(); if (decimal.TryParse(str, out decimal temp)) { return temp; } else { return defaultValue; } } public static double ToDouble(this object value, double defaultValue = 0) { string str = value.ToString(); if (double.TryParse(str, out double temp)) { return temp; } else { return defaultValue; } } public static float ToFloat(this object value, float defaultValue = 0) { string str = value.ToString(); if (float.TryParse(str, out float temp)) { return temp; } else { return defaultValue; } } public static DateTime? ToDateTimeNullable(this object value) { string str = value.ToString(); if (DateTime.TryParse(str, out DateTime temp)) { return temp; } else { return null; } } public static int? ToInt32Nullable(this object value) { string str = value.ToString(); if (int.TryParse(str, out int temp)) { return temp; } else { return null; } } public static long? ToInt64Nullable(this object value) { string str = value.ToString(); if (long.TryParse(str, out long temp)) { return temp; } else { return null; } } public static decimal? ToDecimalNullable(this object value) { string str = value.ToString(); if (decimal.TryParse(str, out decimal temp)) { return temp; } else { return null; } } public static double? ToDoubleNullable(this object value) { string str = value.ToString(); if (double.TryParse(str, out double temp)) { return temp; } else { return null; } } public static float? ToFloatNullable(this object value) { string str = value.ToString(); if (float.TryParse(str, out float temp)) { return temp; } else { return null; } } public static T? Parse(this string value, Func parseFunc) where T : struct { if (string.IsNullOrEmpty(value)) { return null; } else { return parseFunc(value.Trim()); } } /// /// 类型转换 /// public static T Value(this object value) { if (value == null) { return default(T); } if (value is T) return (T)value; else if (value == DBNull.Value) { if (typeof(T) == typeof(DateTime)) { object o = new DateTime(1900, 1, 1); return (T)o; } else return default(T); } else if (value.ToString().ToLower() == "null") return default(T); else return (T)Convert.ChangeType(value, typeof(T)); } public static T Value(this object value, Func funcConvert) { if (value == null) { return default(T); } string s = value.ToString(); if (string.IsNullOrEmpty(s) || s.ToLower() == "null") { return default(T); } return funcConvert(value); } public static object Value(this object value) { return Value(value); } public static object IsNull(this object value, object replaceValue) { if (value == null) { return replaceValue; } else { return value; } } public static T IsNull(this T value, T replaceValue) { if (value.Equals(default(T))) { return replaceValue; } else { return value; } } public static string IsNull(this string value, string replaceValue) { if (string.IsNullOrEmpty(value)) { return replaceValue; } else { return value; } } ///// ///// 去除非法的文件字符 ///// ///// ///// //public static string RemoveInvalidFileChars(this string input) //{ // char[] invalidChars = Path.GetInvalidFileNameChars(); // foreach (char c in invalidChars) // { // input = input.Replace(c.ToString(), ""); // } // return input; //} public static DataTable ToDataTable(this DataSet ds) { try { if (ds == null) { return null; } else { return ds.Tables[0]; } } catch (Exception) { return null; } } public static string ToMonth(this int month, bool isAddPrefix = true, string Suffix = "") { var str = string.Empty; if (isAddPrefix) { str += "F"; } switch (month) { case 1: str += "JANUARY"; break; case 2: str += "FEBRUARY"; break; case 3: str += "MARCH"; break; case 4: str += "APRIL"; break; case 5: str += "MAY"; break; case 6: str += "JUNE"; break; case 7: str += "JULY"; break; case 8: str += "AUGUST"; break; case 9: str += "SEPTEMBER"; break; case 10: str += "OCTOBER"; break; case 11: str += "NOVEMBER"; break; case 12: str += "DECEMBER"; break; } if (!string.IsNullOrEmpty(Suffix)) str += Suffix; return str; } public static string ToMonthLower(this int month, bool isAddPrefix = true, string Suffix = "") { var str = string.Empty; if (isAddPrefix) { str += "F"; } switch (month) { case 1: str += "January"; break; case 2: str += "February"; break; case 3: str += "March"; break; case 4: str += "April"; break; case 5: str += "May"; break; case 6: str += "June"; break; case 7: str += "July"; break; case 8: str += "August"; break; case 9: str += "September"; break; case 10: str += "October"; break; case 11: str += "November"; break; case 12: str += "December"; break; } if (!string.IsNullOrEmpty(Suffix)) str += Suffix; return str; } public static string ToStringExt(this object value) { return ValueToString(value, false); } public static string ValueToString(this object value, bool removeLines) { try { if (value == null) { return string.Empty; } string s = value.ToString(); if (string.IsNullOrEmpty(s) || s.ToLower() == "null") { return string.Empty; } if (removeLines) { s = s.Replace("\r", " ").Replace("\n", " ").Replace("
", " ").Replace("
", " ").Replace("
", " ").Replace("\t", " "); } return s; } catch (Exception) { return string.Empty; } } } }