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. PresentViewController切换界面

    视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...

  2. 基于git diff进行的eslint代码检测

    缘起 在项目中, 通常都会使用代码检测工具来规范团队的代码风格, 比如eslint.随着代码的不断增加, eslint进行代码检测的时间也越来越久.每次检测的时候, 需要检测的文件和实际检测的文件极度 ...

  3. 获取WIFI密码

    在十年前,我还在上初中,班上只有极少数的富二代用得起手机:几年后诺基亚.摩托罗拉.三星手机开始盛行:近些年,安卓.苹果系统手机占据了基本整个市场,WIFI出变得越来越重要. Wifi万能钥匙数据库存储 ...

  4. C# 在執行程式目錄下產生文件夾

    //產生一個Log文件夾string appPath = Application.StartupPath; if (!Directory.Exists(appPath + "/log&quo ...

  5. GitHub学习心得之 简单操作

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 前言 本文对Github的基本操作进行了总结, 主要基于以下文章: http://gitre ...

  6. React Native开发入门

    目录: 一.前言 二.什么是React Native 三.开发环境搭建 四.预备知识 五.最简单的React Native小程序 六.总结 七.参考资料   一.前言 虽然只是简单的了解了一下Reac ...

  7. Android进度条学习

    自定义属性 <!-- roundColor 圆环的颜色 roundProgressColor 进度的颜色 roundWidth 圆环的宽度 textColor 文字颜色 textSize 文字大 ...

  8. AtomicInteger源码注释

    AtomicInteger源码 在java.util.concurrent.atomic包下提供了大量的原子类,这里以AtomicInteger源码为例,添加了一些注释,个人理解,供参考: 其中比较重 ...

  9. 杀毒软件导致YourSQLDba备份失败

    有一朋友咨询我,他配置的YourSQLDba 备份作业经常出现备份失败的情况,我帮忙查看了一下,首先从告警邮件中查看出错的具体信息,执行了下面SQL语句(来自于告警邮件) Exec YourSQLDb ...

  10. .net C# SqlHelper for Oracle

    适用于Oracle的sqlhelper 需要使用ODP.Net,引用Oracle.DataAccess.dll 推荐安装ODAC 代码如下: using System; using System.Co ...