using SqlSugar;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Numerics;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace ServiceCenter.Extensions
{
///
/// 类型转换扩展
///
public static class TypeExtension
{
///
/// 将字符串转换为short
///
/// 需要转换的字符串
///
public static short ToShort(this string value)
{
return Convert.ToInt16(value);
}
///
/// 将int转换为short
///
/// 需要转换的字符串
///
public static short ToShort(this int value)
{
return Convert.ToInt16(value);
}
///
/// 将decimal转换为short
///
/// 需要转换的字符串
///
public static short ToShort(this decimal value)
{
return Convert.ToInt16(value);
}
///
/// 将字符串转换为int
///
/// 需要转换的字符串
///
public static int ToInt(this string value)
{
return Convert.ToInt32(value);
}
///
/// 判断值为奇数/偶数
///
/// 需要判断的值
/// true:是奇数 false:是偶数
public static bool OddNumberOrEven(this short value)
{
return value % 2 != 0;
}
///
/// 获取short类型Code,只限设备组
///
///
///
public static short GetShortCode(this string value)
{
return value.Replace("G", "").ToShort();
}
///
/// 数据映射
///
///
///
///
///
public static D Mapper(S s)
{
var d = Activator.CreateInstance();
var sType = s?.GetType();
var dType = typeof(D);
foreach (var sP in sType.GetProperties())
{
foreach (var dP in dType.GetProperties())
{
if (dP.Name == sP.Name)
{
dP.SetValue(d, sP.GetValue(s));
break;
}
}
}
return d;
}
///
/// 获取字典
///
///
///
///
///
///
public static Dictionary EntityClassToDictionary(T t)
{
var type = typeof(SugarColumn);
Dictionary d = new Dictionary();
var sType = t.GetType();
foreach (var sP in sType.GetProperties())
{
if (sP.CustomAttributes.Any(v => v.AttributeType == type) && sP.Name != "VER" && sP.Name != "ID")
{
d.Add(sP.Name, sP.GetValue(t));
}
}
return d;
}
///
/// 获取MD5字符串
///
///
///
public static string GetMD5(this string myString)
{
var md5 = MD5.Create();
var fromData = Encoding.Unicode.GetBytes(myString);
var targetData = md5.ComputeHash(fromData);
string byte2String = null;
for (var i = 0; i < targetData.Length; i++)
{
byte2String += targetData[i].ToString("x");
}
return byte2String;
}
///
/// DataTable转换成实体类
///
///
///
///
public static List