123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using Log;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Security.Cryptography;
- namespace WCS.BaseExtensions
- {
- /// <summary>
- ///
- /// </summary>
- public static class TypeExtension
- {
- /// <summary>
- /// 将字符串转换为short
- /// </summary>
- /// <param name="value">需要转换的字符串</param>
- /// <returns></returns>
- public static short ToShort(this string value)
- {
- return Convert.ToInt16(value);
- }
- /// <summary>
- /// 将int转换为short
- /// </summary>
- /// <param name="value">需要转换的字符串</param>
- /// <returns></returns>
- public static short ToShort(this int value)
- {
- return Convert.ToInt16(value);
- }
- /// <summary>
- /// 将decimal转换为short
- /// </summary>
- /// <param name="value">需要转换的字符串</param>
- /// <returns></returns>
- public static short ToShort(this decimal value)
- {
- return Convert.ToInt16(value);
- }
- /// <summary>
- /// 将字符串转换为int
- /// </summary>
- /// <param name="value">需要转换的字符串</param>
- /// <returns></returns>
- public static int ToInt(this string value)
- {
- return Convert.ToInt32(value);
- }
- /// <summary>
- /// 判断值为奇数/偶数
- /// </summary>
- /// <param name="value">需要判断的值</param>
- /// <returns> true:是奇数 false:是偶数</returns>
- public static bool OddNumberOrEven(this short value)
- {
- return value % 2 != 0;
- }
- /// <summary>
- /// 获取short类型Code,只限设备组
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static short GetShortCode(this string value)
- {
- return value.Replace("G", "").ToShort();
- }
- /// <summary>
- /// 数据映射
- /// </summary>
- /// <typeparam name="D"></typeparam>
- /// <typeparam name="S"></typeparam>
- /// <param name="s"></param>
- /// <returns></returns>
- public static D Mapper<D, S>(S s)
- {
- D d = Activator.CreateInstance<D>();
- var sType = s.GetType();
- var dType = typeof(D);
- foreach (PropertyInfo sP in sType.GetProperties())
- {
- foreach (PropertyInfo dP in dType.GetProperties())
- {
- if (dP.Name == sP.Name)
- {
- dP.SetValue(d, sP.GetValue(s));
- break;
- }
- }
- }
- return d;
- }
- /// <summary>
- /// 获取字典
- /// </summary>
- /// <typeparam name="T1"></typeparam>
- /// <typeparam name="T2"></typeparam>
- /// <typeparam name="T3"></typeparam>
- /// <param name="t3"></param>
- /// <returns></returns>
- public static Dictionary<string, object> EntityClassToDictionary<T>(T t)
- {
- Type type = typeof(SugarColumn);
- Dictionary<string, object> d = new Dictionary<string, object>();
- var sType = t.GetType();
- foreach (PropertyInfo 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;
- }
- /// <summary>
- /// 获取MD5字符串
- /// </summary>
- /// <param name="myString"></param>
- /// <returns></returns>
- public static string GetMD5(this string myString)
- {
- MD5 md5 = MD5.Create();
- byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
- byte[] targetData = md5.ComputeHash(fromData);
- string byte2String = null;
- for (int i = 0; i < targetData.Length; i++)
- {
- byte2String += targetData[i].ToString("x");
- }
- return byte2String;
- }
- /// <summary>
- /// DataTable转换成实体类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static List<object> TableToEntity(this DataTable dt, string typeName)
- {
- List<object> list = new List<object>();
- try
- {
- foreach (DataRow row in dt.Rows)
- {
- Type entity = Type.GetType(typeName);
- PropertyInfo[] pArray = entity.GetType().GetProperties();
- foreach (PropertyInfo p in pArray)
- {
- if (dt.Columns.Contains(p.Name))
- {
- if (!p.CanWrite) continue;
- var value = row[p.Name];
- if (value != DBNull.Value)
- {
- Type targetType = p.PropertyType;
- Type convertType = targetType;
- if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- //可空类型
- NullableConverter nullableConverter = new NullableConverter(targetType);
- convertType = nullableConverter.UnderlyingType;
- }
- if (!string.IsNullOrEmpty(convertType.FullName) && !string.IsNullOrEmpty(value.ToString()))
- {
- value = Convert.ChangeType(value, convertType);
- }
- switch (convertType.FullName)
- {
- case "System.Decimal":
- p.SetValue(entity, Convert.ToDecimal(value), null);
- break;
- case "System.String":
- p.SetValue(entity, Convert.ToString(value), null);
- break;
- case "System.Int32":
- p.SetValue(entity, Convert.ToInt32(value), null);
- break;
- case "System.Int64":
- p.SetValue(entity, Convert.ToInt64(value), null);
- break;
- case "System.Int16":
- p.SetValue(entity, Convert.ToInt16(value), null);
- break;
- case "System.Double":
- p.SetValue(entity, Convert.ToDouble(value), null);
- break;
- case "System.Single":
- p.SetValue(entity, Convert.ToSingle(value), null);
- break;
- case "System.DateTime":
- p.SetValue(entity, Convert.ToDateTime(value), null);
- break;
- default:
- p.SetValue(entity, value, null);
- break;
- }
- }
- }
- }
- list.Add(entity);
- }
- }
- catch (Exception ex)
- {
- InfoLog.INFO_ERROR("Table转换实体类失败:" + ex.Message);
- }
- return list;
- }
- }
- }
|