123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace IoTIntegrationPlatform.Common
- {
- /// <summary>
- /// 实体映射方法
- /// </summary>
- public static class Mapping
- {
- #region datareader向实体映射
- /// <summary>
- /// DataReader转泛型
- /// </summary>
- /// <typeparam name="T">传入的实体类</typeparam>
- /// <param name="objReader">DataReader对象</param>
- /// <returns></returns>
- public static IList<T> ReaderToList<T>(this IDataReader objReader)
- {
- using (objReader)
- {
- List<T> list = new List<T>();
- //获取传入的数据类型
- Type modelType = typeof(T);
- //遍历DataReader对象
- while (objReader.Read())
- {
- //使用与指定参数匹配最高的构造函数,来创建指定类型的实例
- T model = Activator.CreateInstance<T>();
- for (int i = 0; i < objReader.FieldCount; i++)
- {
- //判断字段值是否为空或不存在的值
- if (!IsNullOrDBNull(objReader[i]))
- {
- //匹配字段名
- PropertyInfo pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
- if (pi != null)
- {
- //绑定实体对象中同名的字段
- pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null);
- }
- }
- }
- list.Add(model);
- }
- return list;
- }
- }
- /// <summary>
- /// 对可空类型进行判断转换(*要不然会报错)
- /// </summary>
- /// <param name="value">DataReader字段的值</param>
- /// <param name="conversionType">该字段的类型</param>
- /// <returns></returns>
- private static object CheckType(object value, Type conversionType)
- {
- if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- if (value == null)
- return "";
- System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
- conversionType = nullableConverter.UnderlyingType;
- }
- return Convert.ChangeType(value, conversionType);
- }
- /// <summary>
- /// 判断指定对象是否是有效值
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- private static bool IsNullOrDBNull(object obj)
- {
- return (obj == null || (obj is DBNull)) ? true : false;
- }
- /// <summary>
- /// DataReader转模型
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="objReader"></param>
- /// <returns></returns>
- public static T ReaderToModel<T>(this IDataReader objReader)
- {
- using (objReader)
- {
- if (objReader.Read())
- {
- Type modelType = typeof(T);
- int count = objReader.FieldCount;
- T model = Activator.CreateInstance<T>();
- for (int i = 0; i < count; i++)
- {
- if (!IsNullOrDBNull(objReader[i]))
- {
- PropertyInfo pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
- if (pi != null)
- {
- pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null);
- }
- }
- }
- return model;
- }
- }
- return default(T);
- }
- /// <summary>
- /// 实体数据映射 AB实体相互映射数据
- /// </summary>
- /// <typeparam name="R">目标实体</typeparam>
- /// <typeparam name="T">数据源实体</typeparam>
- /// <param name="model">数据源</param>
- /// <returns></returns>
- public static R EntityMapping<R, T>(T model)
- {
- R result = Activator.CreateInstance<R>();
- foreach (PropertyInfo info in typeof(R).GetProperties())
- {
- PropertyInfo pro = typeof(T).GetProperty(info.Name);
- if (pro != null)
- {
- info.SetValue(result, pro.GetValue(model));
- }
- }
- return result;
- }
- /// <summary>
- /// Datatable 实体转换为实体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static T DataTableToModel<T>(DataTable dt)
- {
- // 定义实体
- T t = Activator.CreateInstance<T>();
- // 获得此模型的类型
- Type type = typeof(T);
- foreach (DataRow dr in dt.Rows)
- {
- // 获得此模型的公共属性
- PropertyInfo[] propertys = t.GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- PropertyInfo[] propertys1 = pi.GetType().GetProperties();
- var attribute = pi.GetCustomAttributes(typeof(JsonPropertyAttribute), false).FirstOrDefault();
- string proname = ((JsonPropertyAttribute)attribute).PropertyName;
- if (dt.Columns.Contains(proname))
- {
- // 判断此属性是否有Setter
- if (!pi.CanWrite) continue;
- object value = dr[proname];
- if (value != DBNull.Value)
- {
- pi.SetValue(t, CheckType(value, pi.PropertyType), null);
- }
- }
- }
- break;
- }
- return t;
- }
- /// <summary>
- /// 模型转Json字符串
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static string ModeltoJson<T>(T Model)
- {
- string json = string.Empty;
- // 定义实体
- T t = Activator.CreateInstance<T>();
- // 获得此模型的类型
- Type type = typeof(T);
- // 获得此模型的公共属性
- PropertyInfo[] propertys = t.GetType().GetProperties();
- PropertyInfo[] Mpropertys = Model.GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- json += "" + pi.Name + ":" + "";
- }
- return json;
- }
- #endregion
- }
- }
|