You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
13 KiB
C#

7 months ago
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
{
/// <summary>
/// 将对象转换为字符串
/// </summary>
/// <param name="obj">要转换的对象</param>
/// <returns>转换后的string类型结果</returns>
public static string ObjectToStr(object obj)
{
if (obj == null)
return "";
return obj.ToString().Trim();
}
/// <summary>
/// 将对象转换为Int类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static int ObjToInt(object obj)
{
if (isInt(obj))
{
return int.Parse(obj.ToString());
}
else
{
return 0;
}
}
/// <summary>
/// 判断对象是否可以转成int型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// Object型转换为decimal型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的decimal类型结果</returns>
public static decimal ObjToDecimal(object expression, decimal defValue)
{
if (expression != null)
return StrToDecimal(expression.ToString(), defValue);
return defValue;
}
/// <summary>
/// Object型转换为decimal型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的decimal类型结果</returns>
public static double ObjToDouble(object expression, double defValue)
{
if (expression != null)
return StrToDouble(expression.ToString(), defValue);
return defValue;
}
/// <summary>
/// string型转换为decimal型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的decimal类型结果</returns>
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;
}
/// <summary>
/// string型转换为decimal型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的decimal类型结果</returns>
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;
}
/// <summary>
/// 将对象转换为日期时间类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static DateTime StrToDateTime(string str, DateTime defValue)
{
if (!string.IsNullOrEmpty(str))
{
DateTime dateTime;
if (DateTime.TryParse(str, out dateTime))
return dateTime;
}
return defValue;
}
/// <summary>
/// 判断输入的字符串是否是数字
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <returns>是true或者否false</returns>
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);
}
/// 判断一个字符串是否为合法数字(指定整数位数和小数位数)
/// </summary>
/// <param name="s">字符串</param>
/// <param name="precision">整数位数</param>
/// <param name="scale">小数位数</param>
/// <returns></returns>
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);
}
/// <summary>
/// 判断输入的字符串是否只包含英文字母
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 比较两个DataTable内容是否相等先是比数量数量相等就比内容
/// </summary>
/// <param name= "dtA "> </param>
/// <param name= "dtB "> </param>
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;
}
}
/// <summary>
/// 比较两个字段集合是否名称,数据类型一致
/// </summary>
/// <param name= "dcA "> </param>
/// <param name= "dcB "> </param>
/// <returns> </returns>
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;
}
}
/// <summary>
/// 利用反射将DataTable转换为List<T>对象
/// </summary>
/// <param name="dt">DataTable 对象</param>
/// <returns>List<T>集合</returns>
public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
{
// 定义集合
List<T> ts = new List<T>();
//定义一个临时变量
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;
}
/// <summary>
/// 获得32位字符长度的ID
/// </summary>
public static string getGuid()
{
Guid guid = Guid.NewGuid();
return guid.ToString().Replace("-", "").ToUpper();
}
/// <summary>
/// 获取字符串的实际数据库长度
/// </summary>
/// <param name="str">传入的字符串</param>
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;
}
}
}