ConvertHelper 通用类
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 通用类的更多相关文章
- poi导出excel通用类
一.关键的通用类public class PoiExportUtils { private static HSSFWorkbook workBook; public PoiExportUtils ...
- NPOI MVC 模型导出Excel通用类
通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...
- MVC NPOI Linq导出Excel通用类
之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
- mongdo通用类(C#版)
日前从公司离职,很快,还没休息就步入了现在的公司,开始跟着公司的脚步走. 公司的项目基本都是大数据的,所以在数据库上大部分都是使用Mongodb和Redis,基本都是Nosql型的数据库为主.以前自己 ...
- 我写的一个ExcelHelper通用类,可用于读取或生成数据
读取或生成EXCEL数据的方法有很多,一般常见的有: 1.通过OFFICE EXCEL组件,优点:读取与生成EXCEL文件方便,缺点:服务器上必须安装OFFICE软件,且进程无法及时释放 2.通过第三 ...
- DataTable转List<Model>通用类
/// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...
- Memcached通用类(基于enyim.com Memcached Client)
一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP.如下图: <?xml version="1.0"?> <configuratio ...
- Memcached通用类(基于Memcached Client Library)
分享下自己编写的Memcached通用类.欢迎大家帮忙指点下哈~ 使用的是.NET memcached client library 客户端+Memcached Providers using Sys ...
随机推荐
- jQuery构造函数init参数分析(一)
在我的上一篇随笔里面分析了jQuery的构造函数,jQuery对象中有一个原型方法init才是是真正的构造函数,通过init的原型对象跟jQuery的原型对象保持引用关系使得init的实例可以正常调用 ...
- input输入框限制仅能输入数字且规定数字长度(使用与输入手机号)
现在越来越多的账户名使用手机号来登录,为了减少前后端的交互,需要用户在输入时就要进行格式的判断, 目前的常规办法是,在输入完成后进行判断. 下面的方法是在输入时就规定只能输入数字,其他格式的字符是无法 ...
- sharepoint2013的审核日志的时间区域设置
最近在项目中碰到审核日志的时间为GMT格式 ,如何格式化成本地时间. 网站集设置成本地区域无法解决此类问题,后来查询资料才知道.无法更改 Change Audit Time From GMT Time ...
- EA(企业架构)落地之道
随着业务和IT的不断融合,企业业务.信息系统和技术结合日益紧密,企业对信息系统及技术的敏捷性要求越来越高.如何在多变的环境下快速创新产品或服务并推向市场是企业面临的日益紧迫的压力.企业架构作为连接和匹 ...
- Xcode插件优缺点对比(推荐20款插件)
本文大致整理了自己用过的一些插件的使用感想(就是好不好用). 在那之前先简单贴两条插件须知,知道的可以忽略. 1.Alcatraz 类似于管理第三方库的cocoapods,管理插件也有个Alcatra ...
- elasticsearch GIS空间查询问题解决
在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询 我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...
- android:使用RemoteView自定义Notification
//网上相关内容较少,遂记录下来,备忘. //依然以音乐播放器demo为例. 效果截图 //锤子手机上的效果 step1 准备自定义layout 常规的实现方式,并不会因为是用于notificatio ...
- (二)Maven的安装与环境配置
想要安装 Apache Maven在Windows 系统上, 需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : 1.JDK 2.Ma ...
- 最新版powerdesign16.5连接数据库错误解决
由于工作需要,需要将数据库中的表结构逆向生成到powerdesign中,但是连接数据库一直连接不上,Connection test failed报如下错误: 解决方案: 下载ojdbc14.jar,拷 ...
- 【Linux学习】Vi 操作命令集合
Vi 操作命令集合 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文 ...