using Newtonsoft.Json;
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;
using System.Text.RegularExpressions;
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);
}
///
/// 获取最后一位数字
///
/// 字符串
///
public static int GetLastDigit(this string value)
{
return Convert.ToInt32(Regex.Match(value, @"\d+$").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();
}
///
/// 扩展方法,获得枚举的Description
///
/// 枚举值
/// 当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用
/// 枚举的Description
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true)
{
return name;
}
return attribute?.Description;
}
///
/// 获取属性的描述信息
///
public static string GetDescription(this Type type, string proName)
{
PropertyInfo pro = type.GetProperty(proName);
string des = proName;
if (pro != null)
{
des = pro.GetDescription();
}
return des;
}
public static string JsonToString(this object value)
{
return JsonConvert.SerializeObject(value);
}
///
/// 获取属性的描述信息
///
public static string GetDescription(this MemberInfo info)
{
var attrs = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
string des = info.Name;
foreach (DescriptionAttribute attr in attrs)
{
des = attr.Description;
}
return des;
}
///
/// 数据映射
///
/// 映射目标实体Type
///
///
///
public static D Mapper(this 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 object Copy(this object obj, Type type)
{
var res = Activator.CreateInstance(type);
foreach (var p in type.GetProperties())
{
var p2 = obj.GetType().GetProperty(p.Name);
if (p2 != null && p2.PropertyType == p.PropertyType)
{
var a = p2.GetValue(obj);
p.SetValue(res, p2.GetValue(obj));
}
}
return res;
}
///
/// 获取字典
///
///
///
///
///
///
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