数据层反射对象
来源:互联网 发布:mac版梦幻西游更新失败 编辑:程序博客网 时间:2024/04/23 19:20
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace OSell.Utility.DBHelper
{
/// <summary>
/// Mapper
/// </summary>
/// <remarks>
/// </remarks>
public class Mapper
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ToEntity<T>(DataTable dt)
{
try
{
List<T> list = new List<T>();
if (dt != null)
{
foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>();
PropertyInfo[] properties = entity.GetType().GetProperties();
foreach (PropertyInfo info in properties)
{
if (dt.Columns.Contains(info.Name) && info.CanWrite)
{
object objValue = row[info.Name];
if (objValue != DBNull.Value)
{
if (info.PropertyType == typeof(DateTime?) || info.PropertyType == typeof(DateTime))
{
DateTime date = DateTime.MaxValue;
DateTime.TryParse(objValue.ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
info.SetValue(entity, date, null);
}
else if (info.PropertyType == typeof(decimal?) || info.PropertyType == typeof(decimal))
{
Decimal de = 0M;
Decimal.TryParse(objValue.ToString(), out de);
info.SetValue(entity, de, null);
}
else
{
info.SetValue(entity, objValue, null);
}
}
else
{
info.SetValue(entity, null, null);
}
}
}
list.Add(entity);
}
}
return list;
}
catch (Exception ex)
{
return null;
}
}
public static object ToEntity(DataRow adaptedrow, Type entitytype)
{
if (entitytype == null || adaptedrow == null)
{
return null;
}
object entity = Activator.CreateInstance(entitytype);
CopyToEntity(entity, adaptedrow);
return entity;
}
public static T ToEntity<T>(DataRow adaptedrow, T value) where T : new()
{
T item = new T();
if (value == null || adaptedrow == null)
{
return item;
}
item = Activator.CreateInstance<T>();
CopyToEntity(item, adaptedrow);
return item;
}
public static void CopyToEntity(object entity, DataRow adaptedrow)
{
if (entity == null || adaptedrow == null)
{
return;
}
PropertyInfo[] propertyinfos = entity.GetType().GetProperties();
foreach (PropertyInfo propertyinfo in propertyinfos)
{
if (!CansetPropertyValue(propertyinfo, adaptedrow))
{
continue;
}
try
{
if (adaptedrow[propertyinfo.Name] is DBNull)
{
propertyinfo.SetValue(entity, null, null);
continue;
}
SetPropertyValue(entity, adaptedrow, propertyinfo);
}
finally
{
}
}
}
public static bool CansetPropertyValue(PropertyInfo propertyinfo, DataRow adaptedrow)
{
if (!propertyinfo.CanWrite)
{
return false;
}
if (!adaptedrow.Table.Columns.Contains(propertyinfo.Name))
{
return false;
}
return true;
}
public static void SetPropertyValue(object entity, DataRow adaptedrow, PropertyInfo propertyinfo)
{
if (propertyinfo.PropertyType == typeof(DateTime?) || propertyinfo.PropertyType == typeof(DateTime))
{
DateTime date = DateTime.MaxValue;
DateTime.TryParse(adaptedrow[propertyinfo.Name].ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
propertyinfo.SetValue(entity, date, null);
}
else
{
if (propertyinfo.PropertyType == typeof(decimal?) || propertyinfo.PropertyType == typeof(decimal))
{
Decimal de = 0M;
Decimal.TryParse(adaptedrow[propertyinfo.Name].ToString(), out de);
propertyinfo.SetValue(entity, de, null);
}
else
{
propertyinfo.SetValue(entity, adaptedrow[propertyinfo.Name], null);
}
}
}
}
}
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace OSell.Utility.DBHelper
{
/// <summary>
/// Mapper
/// </summary>
/// <remarks>
/// </remarks>
public class Mapper
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ToEntity<T>(DataTable dt)
{
try
{
List<T> list = new List<T>();
if (dt != null)
{
foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>();
PropertyInfo[] properties = entity.GetType().GetProperties();
foreach (PropertyInfo info in properties)
{
if (dt.Columns.Contains(info.Name) && info.CanWrite)
{
object objValue = row[info.Name];
if (objValue != DBNull.Value)
{
if (info.PropertyType == typeof(DateTime?) || info.PropertyType == typeof(DateTime))
{
DateTime date = DateTime.MaxValue;
DateTime.TryParse(objValue.ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
info.SetValue(entity, date, null);
}
else if (info.PropertyType == typeof(decimal?) || info.PropertyType == typeof(decimal))
{
Decimal de = 0M;
Decimal.TryParse(objValue.ToString(), out de);
info.SetValue(entity, de, null);
}
else
{
info.SetValue(entity, objValue, null);
}
}
else
{
info.SetValue(entity, null, null);
}
}
}
list.Add(entity);
}
}
return list;
}
catch (Exception ex)
{
return null;
}
}
public static object ToEntity(DataRow adaptedrow, Type entitytype)
{
if (entitytype == null || adaptedrow == null)
{
return null;
}
object entity = Activator.CreateInstance(entitytype);
CopyToEntity(entity, adaptedrow);
return entity;
}
public static T ToEntity<T>(DataRow adaptedrow, T value) where T : new()
{
T item = new T();
if (value == null || adaptedrow == null)
{
return item;
}
item = Activator.CreateInstance<T>();
CopyToEntity(item, adaptedrow);
return item;
}
public static void CopyToEntity(object entity, DataRow adaptedrow)
{
if (entity == null || adaptedrow == null)
{
return;
}
PropertyInfo[] propertyinfos = entity.GetType().GetProperties();
foreach (PropertyInfo propertyinfo in propertyinfos)
{
if (!CansetPropertyValue(propertyinfo, adaptedrow))
{
continue;
}
try
{
if (adaptedrow[propertyinfo.Name] is DBNull)
{
propertyinfo.SetValue(entity, null, null);
continue;
}
SetPropertyValue(entity, adaptedrow, propertyinfo);
}
finally
{
}
}
}
public static bool CansetPropertyValue(PropertyInfo propertyinfo, DataRow adaptedrow)
{
if (!propertyinfo.CanWrite)
{
return false;
}
if (!adaptedrow.Table.Columns.Contains(propertyinfo.Name))
{
return false;
}
return true;
}
public static void SetPropertyValue(object entity, DataRow adaptedrow, PropertyInfo propertyinfo)
{
if (propertyinfo.PropertyType == typeof(DateTime?) || propertyinfo.PropertyType == typeof(DateTime))
{
DateTime date = DateTime.MaxValue;
DateTime.TryParse(adaptedrow[propertyinfo.Name].ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
propertyinfo.SetValue(entity, date, null);
}
else
{
if (propertyinfo.PropertyType == typeof(decimal?) || propertyinfo.PropertyType == typeof(decimal))
{
Decimal de = 0M;
Decimal.TryParse(adaptedrow[propertyinfo.Name].ToString(), out de);
propertyinfo.SetValue(entity, de, null);
}
else
{
propertyinfo.SetValue(entity, adaptedrow[propertyinfo.Name], null);
}
}
}
}
}
0 0
- 数据层反射对象
- 泛型+反射实现数据层+业务层
- java反射示例:数据库数据反射为java对象
- java反射示例:数据库数据反射为java对象
- 对象之间通过反射拷贝数据
- Java反射复制一个数据对象
- 通过反射进行数据访问层的抽象
- 通过反射进行数据访问层的抽象
- 基于泛型反射的通用数据层封装
- DNN中怎么使用反射创建数据访问层Provider
- C3P0+反射注释+dbutils搭建数据访问层
- 基于泛型反射的通用数据层封装
- java减少数据访问层代码—反射1—数据访问层Wrapper
- 持久层与对象数据映射(ORM)
- cocos2dx tile地图获取对象层数据
- Java中基于HttpServlet的反射机制(封装从view层页面接收到的数据于实体对象中)
- 抽象工厂+反射+依赖注入 实现对数据访问层和业务逻辑层的优化
- 抽象工厂+反射+依赖注入 实现对数据访问层和业务逻辑层的优化
- C\C++代码优化的27个建议
- OCP 1Z0 051 127
- cc150第一章JAVA字符串和数组总结
- 诸葛蝴蝶
- linux下mysql安装配置
- 数据层反射对象
- 2、Cocos2dx 3.0游戏开发找小三之引擎简介
- 【Java】深入Java多线程和并发编程之ReentrantLock
- SAP培训学校的选择
- 组播地址划分
- 96 Android 将XML文件转换成Java对象
- 有故事的人啊
- JAVA输入输出流操作
- PyDev安装之后却找不到