public class ConvertHelper<T> where T : new()
{
private static Dictionary<Type, List<IPropertyConvertInfo>> ConvertInfo = new Dictionary<Type, List<IPropertyConvertInfo>>(); public static ObservableCollection<T> ConvertToList(DataTable dt)
{
List<IPropertyConvertInfo> convertInfo = ConvertInfo.ContainsKey(typeof(T)) ? ConvertInfo[typeof(T)] : ParseConvertInfo(typeof(T));
convertInfo = convertInfo.Where(v => dt.Columns.Contains(v.ColumnName)).ToList();
return new ObservableCollection<T>(dt.Rows.Cast<DataRow>().Select(v => ConvertRowToModel(convertInfo, v)));
} #region ==== Private Mothods ==== private static List<IPropertyConvertInfo> ParseConvertInfo(Type modelType)
{
var properties = modelType.GetProperties().Where(v => v.IsDefined(typeof(ColumnNameAttribute), true))
.Select(v => CreatePropertyConvertInfo(v)).ToList();
ConvertInfo[modelType] = properties;
return properties;
} private static IPropertyConvertInfo CreatePropertyConvertInfo(PropertyInfo property)
{
IPropertyConvertInfo info = Activator.CreateInstance(typeof(PropertyConvertInfo<>).MakeGenericType(property.PropertyType)) as IPropertyConvertInfo;
info.ColumnName = ((ColumnNameAttribute)property.GetCustomAttributes(typeof(ColumnNameAttribute), true)[]).ColumnName;
info.Property = property;
return info;
} private static T ConvertRowToModel(List<IPropertyConvertInfo> convertInfo, DataRow dataRow)
{
T model = new T();
convertInfo.ForEach(v => v.DoFetchData(model, dataRow));
return model;
} #endregion ==== Private Mothods ====
} internal interface IPropertyConvertInfo
{
PropertyInfo Property { get; set; }
string ColumnName { get; set; }
void DoFetchData(object model, DataRow row);
}
internal class PropertyConvertInfo<TProperty> : IPropertyConvertInfo
{
public PropertyInfo Property { get; set; }
public string ColumnName { get; set; } public void DoFetchData(object model, DataRow row)
{
if (!(row[ColumnName] is DBNull))
{
if (row[ColumnName].GetType().ToString() == "System.Decimal")
{
object value = Convert.ToDouble(row[ColumnName]);
Property.SetValue(model, value, null);
}
else if (row[ColumnName].GetType().ToString() =="System.SByte")
{
object value = Convert.ToBoolean(row[ColumnName]);
Property.SetValue(model, value, null);
}
else
{
Property.SetValue(model, row.Field<TProperty>(ColumnName), null);
}
}
}
}

ConvertHelper 通用类的更多相关文章

  1. poi导出excel通用类

    一.关键的通用类public class PoiExportUtils {    private static HSSFWorkbook workBook; public PoiExportUtils ...

  2. NPOI MVC 模型导出Excel通用类

    通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...

  3. MVC NPOI Linq导出Excel通用类

    之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...

  4. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

  5. mongdo通用类(C#版)

    日前从公司离职,很快,还没休息就步入了现在的公司,开始跟着公司的脚步走. 公司的项目基本都是大数据的,所以在数据库上大部分都是使用Mongodb和Redis,基本都是Nosql型的数据库为主.以前自己 ...

  6. 我写的一个ExcelHelper通用类,可用于读取或生成数据

    读取或生成EXCEL数据的方法有很多,一般常见的有: 1.通过OFFICE EXCEL组件,优点:读取与生成EXCEL文件方便,缺点:服务器上必须安装OFFICE软件,且进程无法及时释放 2.通过第三 ...

  7. DataTable转List<Model>通用类

    /// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...

  8. Memcached通用类(基于enyim.com Memcached Client)

    一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP.如下图: <?xml version="1.0"?> <configuratio ...

  9. Memcached通用类(基于Memcached Client Library)

    分享下自己编写的Memcached通用类.欢迎大家帮忙指点下哈~ 使用的是.NET memcached client library 客户端+Memcached Providers using Sys ...

随机推荐

  1. jquery如何实现(textarea) placeholder自动换行?

    思路:利用文本框的聚焦和失焦事件 1.HTML结构 <textarea id="text1"></textarea> 2.js方法 <script&g ...

  2. 原生JS:严格模式详解

    严格模式 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript) 设计目的 设立”严格模式 ...

  3. XSS跨站测试代码大全

    '><script>alert(document.cookie)</script>='><script>alert(document.cookie)&l ...

  4. 使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First

    [前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费时间往下看了. 记得09年第一次接触ORM————Linq2Sql,从此对她的爱便一发不可收拾,一年后 ...

  5. Android—自定义标题栏的实现及遇见的问题解决

    开发者设计界面时候往往不会使用系统自带的标题栏,因为不美观,所以需要自己设置标题栏. 1.根据需求在xml文件中设置标题布局 <?xml version="1.0" enco ...

  6. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  7. Android View的几个位置坐标关系

    1. View的边界,left, top, right, bottom(即左上右下),这些值都是相对View的父容器说的: 2. View的x, translationX, y, translatio ...

  8. iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿

      iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...

  9. linux 学习随笔-shell基础知识

    1:用户的shell历史命令保存在home/username/.bash_history中 #!!  执行用户的上一条命令 #!pw  执行命令历史中最近一次以pw开头的命令 2:'*'来匹配零或多个 ...

  10. SQLite学习笔记(九)&&pager模块

    概述 通过上一篇文章的分析,我们知道了pager模块在整个sqlite中所处的位置.它是sqlite的核心模块,充当了多种重要角色.作为一个事务管理器,它通过并发控制和故障恢复实现事务的ACID特性, ...