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
{
///
/// 实体映射方法
///
public static class Mapping
{
#region datareader向实体映射
///
/// DataReader转泛型
///
/// 传入的实体类
/// DataReader对象
///
public static IList ReaderToList(this IDataReader objReader)
{
using (objReader)
{
List list = new List();
//获取传入的数据类型
Type modelType = typeof(T);
//遍历DataReader对象
while (objReader.Read())
{
//使用与指定参数匹配最高的构造函数,来创建指定类型的实例
T model = Activator.CreateInstance();
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;
}
}
///
/// 对可空类型进行判断转换(*要不然会报错)
///
/// DataReader字段的值
/// 该字段的类型
///
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);
}
///
/// 判断指定对象是否是有效值
///
///
///
private static bool IsNullOrDBNull(object obj)
{
return (obj == null || (obj is DBNull)) ? true : false;
}
///
/// DataReader转模型
///
///
///
///
public static T ReaderToModel(this IDataReader objReader)
{
using (objReader)
{
if (objReader.Read())
{
Type modelType = typeof(T);
int count = objReader.FieldCount;
T model = Activator.CreateInstance();
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);
}
///
/// 实体数据映射 AB实体相互映射数据
///
/// 目标实体
/// 数据源实体
/// 数据源
///
public static R EntityMapping(T model)
{
R result = Activator.CreateInstance();
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;
}
///
/// Datatable 实体转换为实体
///
///
///
///
public static T DataTableToModel(DataTable dt)
{
// 定义实体
T t = Activator.CreateInstance();
// 获得此模型的类型
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;
}
///
/// 模型转Json字符串
///
///
///
public static string ModeltoJson(T Model)
{
string json = string.Empty;
// 定义实体
T t = Activator.CreateInstance();
// 获得此模型的类型
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
}
}