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#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
}
}
}