using System;using System.Collections;using System.Collections.Generic;using System.Data;using System.Linq;using System.Reflection;using System.Text;using System.Text.RegularExpressions;namespace IBO.XJMYQP.Utility{ public static class Extension { #region String Extension public static string[] Split(this string val, string code) { return Regex.Split(val, code); } public static bool IsNullOrEmpty(this string val) { return string.IsNullOrEmpty(val); } public static bool IsNotEmpty(this string val) { return !string.IsNullOrEmpty(val); } public static string ToString2(this object val) { if (val == null) return string.Empty; return val.ToString(); } ////// 将1900-01-01或 0001/1/1 转换为空字符串 /// /// ///public static string ToVaildDateTime(this object val) { if (val == null) return string.Empty; else if (val.ToString() == "0001/1/1 0:00:00") return string.Empty; else if (val.ToString() == "1900-01-01 00:00:00.000" || val.ToString() == "1900/1/1 0:00:00") return string.Empty; return val.ToString(); } /// /// 将空值和NULL值转换为一个默认的时间1900-01-01 /// /// ///public static string ToInVaildDateTime(this object val) { if (val == null) return "1900-01-01"; else if (val.ToString2().IsNullOrEmpty()) return "1900-01-01"; return val.ToString(); } /// /// 判断字符串是否是数字 /// public static bool IsNumeral(this string val) { decimal i = 0; return decimal.TryParse(val, out i); } ////// 清除空格,加入非空判断,空字符串调用也不会报错 /// public static string TrimSpace(this string val) { if (val == null) return null; else return val.Trim(); } public static bool Contains(this string val, string value, StringComparison comp) { return val.IndexOf(value, comp) >= 0; } public static DateTime? ToDateTime(this string val) { if (string.IsNullOrEmpty(val)) return null; DateTime dt; if (DateTime.TryParse(val, out dt)) { return dt; } return null; } public static bool ToBoolean(this string val) { if (string.IsNullOrEmpty(val)) return false; return val == Boolean.TrueString; } public static int ToInt(this string val) { int intValue; if (int.TryParse(val, out intValue)) { return intValue; } return 0; } public static long ToLong(this string val) { long intValue; if (long.TryParse(val, out intValue)) { return intValue; } return 0; } public static decimal ToDecimal(this string val) { decimal intValue; if (decimal.TryParse(val, out intValue)) { return intValue; } return 0; } public static double ToDouble(this string val) { double result; if (double.TryParse(val, out result)) { return result; } return 0; } public static float ToFloat(this string val) { float result; if (float.TryParse(val, out result)) { return result; } return 0; } ////// 按照长度截取字符串 /// /// /// ///public static string Truncate(this string val, int length) { if (string.IsNullOrEmpty(val)) return val; return (val.Length > length ? val.Substring(0, length - 1) : val); } /// /// 按照长度截取字符串 /// /// /// /// 结尾符号 ///public static string Truncate(this string val, int length, string coda) { if (string.IsNullOrEmpty(val)) return val; return (val.Length > length ? val.Substring(0, length - 1) + coda : val); } public static int ParseDayOfWeek(this string val) { val = val.ToLower(); switch (val) { case "monday": return 1; case "tuesday": return 2; case "wednesday": return 3; case "thursday": return 4; case "friday": return 5; case "saturday": return 6; case "sunday": return 7; default: return 0; } } /// /// 截取指定字节长度的字符串 /// /// 原字符串 /// 截取字节长度 ///public static string CutByteString(this string str, int len) { string result = string.Empty;// 最终返回的结果 if (string.IsNullOrEmpty(str)) { return result; } int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度 int charLen = str.Length;// 把字符平等对待时的字符串长度 int byteCount = 0;// 记录读取进度 int pos = 0;// 记录截取位置 if (byteLen > len) { for (int i = 0; i < charLen; i++) { if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2 { byteCount += 2; } else// 按英文字符计算加1 { byteCount += 1; } if (byteCount > len)// 超出时只记下上一个有效位置 { pos = i; break; } else if (byteCount == len)// 记下当前位置 { pos = i + 1; break; } } if (pos >= 0) { result = str.Substring(0, pos); } } else { result = str; } return result; } /// /// 截取指定字节长度的字符串 /// /// 原字符串 /// 起始位置 /// 截取字节长度 ///public static string CutByteString(this string str, int startIndex, int len) { string result = string.Empty;// 最终返回的结果 if (string.IsNullOrEmpty(str)) { return result; } int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度 int charLen = str.Length;// 把字符平等对待时的字符串长度 if (startIndex == 0) { return CutByteString(str, len); } else if (startIndex >= byteLen) { return result; } else //startIndex < byteLen { int AllLen = startIndex + len; int byteCountStart = 0;// 记录读取进度 int byteCountEnd = 0;// 记录读取进度 int startpos = 0;// 记录截取位置 int endpos = 0;// 记录截取位置 for (int i = 0; i < charLen; i++) { if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2 { byteCountStart += 2; } else// 按英文字符计算加1 { byteCountStart += 1; } if (byteCountStart > startIndex)// 超出时只记下上一个有效位置 { startpos = i; AllLen = startIndex + len - 1; break; } else if (byteCountStart == startIndex)// 记下当前位置 { startpos = i + 1; break; } } if (startIndex + len <= byteLen)//截取字符在总长以内 { for (int i = 0; i < charLen; i++) { if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2 { byteCountEnd += 2; } else// 按英文字符计算加1 { byteCountEnd += 1; } if (byteCountEnd > AllLen)// 超出时只记下上一个有效位置 { endpos = i; break; } else if (byteCountEnd == AllLen)// 记下当前位置 { endpos = i + 1; break; } } endpos = endpos - startpos; } else if (startIndex + len > byteLen)//截取字符超出总长 { endpos = charLen - startpos; } if (endpos >= 0) { result = str.Substring(startpos, endpos); } } return result; } #endregion #region Object Extension public static DateTime? ToDateTime(this object val) { if (val == null) return null; return val.ToString().ToDateTime(); } public static DateTime ToDateTime2(this object val) { if (val == null) return DateTime.MinValue; var time = val.ToString().ToDateTime(); return time.HasValue ? time.Value : DateTime.MinValue; } public static Guid ToGuid(this object val) { //=>3.5没有提供Guid.TryParse 方法。 if (val == null) return Guid.Empty; Guid gd; try { gd = new Guid(val.ToString2()); return gd; } catch { return Guid.Empty; } } public static bool ToBoolean(this object val) { if (val == null) return false; return val.ToString().ToBoolean(); } public static int ToInt(this object val) { if (val == null) return 0; return val.ToString().ToInt(); } public static long ToLong(this object val) { if (val == null) return 0; return val.ToString().ToLong(); } public static int ToIndexInt(this object val) { if (val == null) return -1; return val.ToString().ToInt(); } public static decimal ToDecimal(this object val) { if (val == null) return 0; return val.ToString().ToDecimal(); } public static double ToDouble(this object val) { if (val == null) return 0; return val.ToString().ToDouble(); } public static float ToFloat(this object val) { if (val == null) return 0; return val.ToString().ToFloat(); } /// /// 对象转换为Json /// /// 对象 ///Json字符串 public static string ToJson(this object jsonObject) { string jsonString = "{"; PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties(); for (int i = 0; i < propertyInfo.Length; i++) { object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null); string value = string.Empty; if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan) { value = "'" + objectValue.ToString() + "'"; } else if (objectValue is string) { value = "'" + ToJson(objectValue.ToString()) + "'"; } else if (objectValue is IEnumerable) { value = ToJson((IEnumerable)objectValue); } else { value = ToJson(objectValue.ToString()); } jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ","; } jsonString.Remove(jsonString.Length - 1, jsonString.Length); return jsonString + "}"; } #endregion #region Numeral Extension ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this decimal val, decimal start, decimal end) { return val >= start && val <= end; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this int val, int start, int end) { return val >= start && val <= end; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this float val, float start, float end) { return val >= start && val <= end; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this double val, double start, double end) { return val >= start && val <= end; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this decimal? val, decimal start, decimal end) { return val.HasValue ? (val >= start && val <= end) : false; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this int? val, int start, int end) { return val.HasValue ? (val >= start && val <= end) : false; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this float? val, float start, float end) { return val.HasValue ? (val >= start && val <= end) : false; } ////// 是否在指定范围内 /// /// 起始数值 /// 结束数值 public static bool Between(this double? val, double start, double end) { return val.HasValue ? (val >= start && val <= end) : false; } #endregion #region Data Extension ////// 通用简单实体类型互转 /// public static ListConvertToEntityList (this object list) where ResultType : new() { List ResultList = new List (); if (list == null) return ResultList; Type fromObj = list.GetType(); if (fromObj.Equals(typeof(DataTable))) { var dt = list as DataTable; ResultList = dt.Rows.Cast ().Where(m => !(m.RowState == DataRowState.Deleted || m.RowState == DataRowState.Detached)).Select(m => m.ConvertToEntityByDataRow ()).ToList(); } else if (list is IEnumerable) { ResultList = ((IList)list).Cast