using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; //vs 部分快捷键 /** Ctrl + C :复制 Ctrl + V :粘贴 Ctrl + X :剪切 Ctrl + Z :返回 Ctrl + S :保存 Ctrl + A:全选 Ctrl + B:生成解决方案 Ctrl + O:打开此文件的目录 Ctrl + Shift:+ O:打开项目 F12 :转到定义 F5 :开始调试 Ctrl + F5:开始执行,不调试 Ctrl + F12:转到声明 F1 :帮助 Shift:+ F1:当前窗口帮助 Tab:选项卡向右移(ctrl + A全选或者选择某一区域进行右移) Tab + Shift:选项卡向左移(ctrl + A全选或者选择某一区域进行左移) Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl + M + L: 展开所有方法 Ctrl + M + M:快速隐藏或显示当前代码段 Ctrl + Tab:快速切换不同的窗口 Ctrl + F:快速查找 Ctrl + H:查找替换 Ctrl + G:转到行 Ctrl + T:转到所有 Ctrl + K + C:注释选择的代码 Ctrl + K + U:取消对选择代码的注释 Ctrl + K, Ctrl + F 或 Ctrl + K + D :整理代码 Ctrl + Shift:+ F9 :取消所有断点 Alt + Shift +F10: 添加命名空间 Ctrl+U:将选定行代码变为小写字母 Ctrl+Shift+U:将选定行代码变为大写字母 */ namespace ibk.IPD.Common { public class CommonUtils { /// /// 将对象转换为字符串 /// /// 要转换的对象 /// 转换后的string类型结果 public static string ObjectToStr(object obj) { if (obj == null) return ""; return obj.ToString().Trim(); } /// /// 将对象转换为Int类型 /// /// /// public static int ObjToInt(object obj) { if (isInt(obj)) { return int.Parse(obj.ToString()); } else { return 0; } } /// /// 判断对象是否可以转成int型 /// /// /// public static bool isInt(object o) { int tmpInt; if (o == null) { return false; } if (o.ToString().Trim().Length == 0) { return false; } if (!int.TryParse(o.ToString(), out tmpInt)) { return false; } else { return true; } } /// /// Object型转换为decimal型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的decimal类型结果 public static decimal ObjToDecimal(object expression, decimal defValue) { if (expression != null) return StrToDecimal(expression.ToString(), defValue); return defValue; } /// /// Object型转换为decimal型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的decimal类型结果 public static double ObjToDouble(object expression, double defValue) { if (expression != null) return StrToDouble(expression.ToString(), defValue); return defValue; } /// /// string型转换为decimal型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的decimal类型结果 public static double StrToDouble(string expression, double defValue) { if ((expression == null)) return defValue; double intValue = defValue; if (expression != null) { bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsDecimal) double.TryParse(expression, out intValue); } return intValue; } /// /// string型转换为decimal型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的decimal类型结果 public static decimal StrToDecimal(string expression, decimal defValue) { if ((expression == null)) return defValue; decimal intValue = defValue; if (expression != null) { bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsDecimal) decimal.TryParse(expression, out intValue); } return intValue; } /// /// 将对象转换为日期时间类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static DateTime StrToDateTime(string str, DateTime defValue) { if (!string.IsNullOrEmpty(str)) { DateTime dateTime; if (DateTime.TryParse(str, out dateTime)) return dateTime; } return defValue; } /// /// 判断输入的字符串是否是数字 /// /// 要转换的字符串 /// 是true或者否false public static bool IsNumber(string str) { Regex objNotNumberPattern = new Regex("[^0-9.-]"); Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); return !objNotNumberPattern.IsMatch(str) && !objTwoDotPattern.IsMatch(str) && !objTwoMinusPattern.IsMatch(str) && objNumberPattern.IsMatch(str); } /// 判断一个字符串是否为合法数字(指定整数位数和小数位数) /// /// 字符串 /// 整数位数 /// 小数位数 /// public static bool IsNumber(string s, int precision, int scale) { if ((precision == 0) && (scale == 0)) { return false; } string pattern = @"(^\d{1," + precision + "}"; if (scale > 0) { pattern += @"\.\d{0," + scale + "}$)|" + pattern; } pattern += "$)"; return Regex.IsMatch(s, pattern); } /// /// 判断输入的字符串是否只包含英文字母 /// /// /// public static bool IsEnglish(string input) { //string pattern = @"^[A-Za-z0-9]+$"; 数字和英文字母 string pattern = @"^[A-Za-z]+$"; // 英文字母 Regex regex = new Regex(pattern); return regex.IsMatch(input); } /// /// 比较两个DataTable内容是否相等,先是比数量,数量相等就比内容 /// /// /// public static bool CompareDataTable(DataTable dtA, DataTable dtB) { if (dtA.Rows.Count == dtB.Rows.Count) { if (CompareColumn(dtA.Columns, dtB.Columns)) { //比内容 for (int i = 0; i < dtA.Rows.Count; i++) { for (int j = 0; j < dtA.Columns.Count; j++) { if (!dtA.Rows[i][j].Equals(dtB.Rows[i][j])) { return false; } } } return true; } else { return false; } } else { return false; } } /// /// 比较两个字段集合是否名称,数据类型一致 /// /// /// /// public static bool CompareColumn(System.Data.DataColumnCollection dcA, System.Data.DataColumnCollection dcB) { if (dcA.Count == dcB.Count) { foreach (DataColumn dc in dcA) { //找相同字段名称 if (dcB.IndexOf(dc.ColumnName) > -1) { //测试数据类型 if (dc.DataType != dcB[dcB.IndexOf(dc.ColumnName)].DataType) { return false; } } else { return false; } } return true; } else { return false; } } /// /// 利用反射将DataTable转换为List对象 /// /// DataTable 对象 /// List集合 public static List DataTableToList(DataTable dt) where T : class, new() { // 定义集合 List ts = new List(); //定义一个临时变量 string tempName = string.Empty; //遍历DataTable中所有的数据行 foreach (DataRow dr in dt.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); //遍历该对象的所有属性 foreach (PropertyInfo pi in propertys) { tempName = pi.Name;//将属性名称赋值给临时变量 //检查DataTable是否包含此列(列名==对象的属性名) if (dt.Columns.Contains(tempName)) { //取值 object value = dr[tempName]; //如果非空,则赋给对象的属性 if (value != DBNull.Value) { pi.SetValue(t, value, null); } } } //对象添加到泛型集合中 ts.Add(t); } return ts; } /// /// 获得32位字符长度的ID /// public static string getGuid() { Guid guid = Guid.NewGuid(); return guid.ToString().Replace("-", "").ToUpper(); } /// /// 获取字符串的实际数据库长度 /// /// 传入的字符串 public static int GetLength(string str) { if (str.Length == 0) return 0; ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; byte[] s = ascii.GetBytes(str); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 3; } else { tempLen += 1; } } return tempLen; } } }