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. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  2. XIB 上的控件不显示怎么办

    原文:http://www.cnblogs.com/sandyzhang/p/5660061.html   午休时间遇到有人求助:说是XIB 上内容都有的,但是看不到,demo 运行的话控件都存在的. ...

  3. active_record的不定时更新

    1 valid?方法只是校验所有的校验条件,如果规则A仅加在数据库上,而不是model上,是不对校验起作用的,最后提交时虽然校验成功,但仍然会抛异常.比如数据库中增加了非空的规则,但模型上没有pres ...

  4. TCP三次握手建立连接

    基本过程:       ISN(初始序号)随时间变化,每一个连接具有不同的ISN,防止在网络延迟中分组被重新发送.   请求端发送SYN(同步序号 )=1,seq=ISN(32bits序号,每4ms+ ...

  5. CentOS7 虚拟机搭建、初始设置、简单使用

    注:虚拟机安装的系统是CentOS7 1.网络设置 网络的设置主要是为了让虚拟机和物理机能够相互ping通,这样就可以用XShell之类的工具操控,也可以上网 见另一篇 CentOS7网络配置 2.物 ...

  6. 理解 Glance - 每天5分钟玩转 OpenStack(20)

    OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...

  7. Ubuntu下Apache+SVN+submin实现WEB管理SVN

    为什么需要submin管理SVN? 原来在Ubuntu下,都是直接通过命令行创建SVN仓库并分配权限,但是这有一些问题: 每创建一个SVN仓库,都需要修改httpd.conf 每创建一个帐户,都需要手 ...

  8. 使用JNI封装底层input系统提供的event事件

    首先说下思路,本文采用jni技术封装底层触摸事件,封装成MotionEvent类一样,不过没有android系统MotionEvent强大.源码MotionEvent位置:java-->fram ...

  9. android intent 传递list或者对象

    (转:http://www.cnblogs.com/lee0oo0/archive/2012/09/24/2699805.html) 方法一: 如果单纯的传递List<String> 或者 ...

  10. Linux下使用 Memory Analyzer

    一. 安装Memory Analyzer Tool 打开eclipse >> Help >> Install New Software >> 点击Work With ...