通用扩展函数1.0
ps:提高了编程效率,而且相当好用,自己随时可以添加新的扩展 public static class CommomExtentions { public static bool IsNullOrEmpty(this string str) { return string.IsNullOrEmpty(str); } public static bool IsNotNullOrEmpty(this string str) { return !string.IsNullOrEmpty(str); } public static bool NotContains(this string str, string txt) { return str.IsNotNull() && !str.Contains(txt); } public static string Format2(this string str, params object[] args) { return string.Format(str, args); } /// <summary> /// 判断字符串是否是int类型 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsInt(this string str) { int temp; return int.TryParse(str, out temp); } /// <summary> /// 如果不是int类型,返回0 /// </summary> /// <param name="str"></param> /// <returns></returns> public static int ToInt(this string str) { int temp; return int.TryParse(str, out temp) ? temp : 0; } public static short ToShort(this string str) { short temp; return short.TryParse(str, out temp) ? temp : default(short); } public static long ToLong(this string str) { long temp; return long.TryParse(str, out temp) ? temp : 0; } /// <summary> /// 转换为 string.Trim() 后的值,null or empty 则不会转换 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ToTrim(this string str) { return str.IsNotNullOrEmpty() ? str.Trim() : str; } public static string StringJoin(this IEnumerable<string> list, string separator) { return string.Join(separator, list); } public static bool IsNotNull<T>(this T obj) { return obj != null; } public static bool IsNull<T>(this T obj) { return obj == null; } /// <summary> /// 去重用法 list.Distinct((x, y) => x.Name == y.Name) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <param name="func"></param> /// <returns></returns> public static List<T> Distinct<T>(this IEnumerable<T> list, Func<T, T, bool> func) { var result = new List<T>(); foreach (var item in list) { if (!result.Any(x => func(x, item))) { result.Add(item); } } return result; } public static bool NotAny<T>(this IEnumerable<T> list) { return !list.Any(); } public static bool ToBool(this int num) { return Convert.ToBoolean(num); } public static string ToBase64String(this string str) { return Convert.ToBase64String(Encoding.Default.GetBytes(str)); } //对象映射 public static T2 Map<T2>(this object t1) where T2 : class { var t1_Pinfos = t1.GetType().GetProperties(); var t2_Pinfos = typeof(T2).GetProperties(); var t2 = Activator.CreateInstance<T2>(); foreach (var p1 in t1_Pinfos) { if (t2_Pinfos.Any(x => x.Name.ToLower() == p1.Name.ToLower() && x.MemberType==p1.MemberType)) { var p2 = t2_Pinfos.FirstOrDefault(x => x.Name.ToLower() == p1.Name.ToLower() && x.MemberType == p1.MemberType); p2.SetValue(t2,p1.GetValue(t1)); } } return t2; } public static bool ExistsDirectory(this string str) { return System.IO.Directory.Exists(str); } public static bool ExistsFile(this string str) { return System.IO.File.Exists(str); } public static void OpenDirectory(this string path) { System.Diagnostics.Process.Start(path); } }