C#扩展方法 DataTable.ToEntitys
类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加功能到类A中去,怎么办? 这个时候扩展方法(Extension Methods)就会帮助你完成上述功能了。现在举例如下为DataTable添加一个转ToEntities方法:
扩展方法实现:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace CNN
{
//必需静态类
public static class ExtendClass
{
//必需静态方法,并且使用this关键字修饰
public static IEnumerable<T> ToEntitys<T>(this DataTable @this) where T : new()
{
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
List<T> list = new List<T>();
foreach (DataRow dr in @this.Rows)
{
T entity = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
PropertyInfo[] array = properties;
for (int i = ; i < array.Length; i++)
{
PropertyInfo property = array[i];
if (@this.Columns.Contains(property.Name))
{
Type valueType = property.PropertyType;
property.SetValue(entity, dr[property.Name].To(valueType), null);
}
}
FieldInfo[] array2 = fields;
for (int j = ; j < array2.Length; j++)
{
FieldInfo field = array2[j];
if (@this.Columns.Contains(field.Name))
{
Type valueType2 = field.FieldType;
field.SetValue(entity, dr[field.Name].To(valueType2));
}
}
list.Add(entity);
}
return list;
}
}
}
扩展方法使用:
DataTable dt = GetTable();
var list = dt.ToEntitys<MyEntitie>();
C#扩展方法 DataTable.ToEntitys的更多相关文章
- 扩展方法 DataTable的ToList<T>
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...
- DataTable扩展方法ToList<T>()、ToJSON()、ToArrayList()
/// <summary> /// 扩展方法类 /// </summary> public static class CommonExtension { /// <sum ...
- C#中DataTable与实体集合通用转换(使用扩展方法)
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...
- 【转】给DataTable和DataRow扩展方法,直接转换为对象集合或对象
/// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为对象集合或对象 /// 补充说明:此扩展类可以极大的简化操作,但是性能低下,大数据以 ...
- DataTable和DataRow利用反射直接转换为Model对象的扩展方法类
DataTable和DataRow利用反射直接转换为Model对象的扩展方法类 /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...
- C# DataTable扩展方法
在日常搬砖中,总结了一些简单的扩展方法. public static bool IsNullOrEmpty(this DataTable dt) { ; } public static bool Is ...
- (原创)[C#] DataTable排序扩展方法
一,前言 DataTable的应用极其广泛,对DataTable进行排序也有很多方式,每种的实现方式都不难,但是使用起来却比较繁琐,所以本人便写了一个扩展方法,专门对DataTable进行操作. 本篇 ...
- 分享.NET系统开发过程中积累的扩展方法
.NET 3.5提供的扩展方法特性,可以在不修改原类型代码的情况下扩展它的功能.下面分享的这些扩展方法大部分来自于Code Project或是Stackoverflow,.NET为此还有一个专门提供扩 ...
- 译:泛型List集合转化为DateTable的扩展方法
译文出处:http://www.codeproject.com/Tips/867866/Extension-Method-for-Generic-List-Collection-to-Da 这段代码是 ...
随机推荐
- 录屏状态监听之防录屏 - iOS
继之前接到电话.短信和截屏监听需求之后,在 iOS 11.0 系统之上新增了屏幕录制的新功能玩法,所以也随之迎来了新的屏幕录制监听的需求,即防录屏功能监听 ... 通过官方文档得知 capturedD ...
- New Machine Learning Server for Deep Learning in Nuke(翻译)
最近一直在开发Orchestra Pipeline System,歇两天翻译点文章换换气.这篇文章是无意间看到的,自己从2015年就开始关注机器学习在视效领域的应用了,也曾利用碎片时间做过一些算法移植 ...
- 安装suds,提示No module named 'client'
最近在研究webservice,但是在线安装suds的时候提示No module named 'client' 提示没有client模块,提示这个错误主要还是因为没有安装client模块 在线安装cl ...
- flask-migrate的使用
先安装flask-migrate:pip install flask-migrate 然后见代码: 输入命令生成migrates文件夹 然后可以看到项目下生成文件夹: 然后输入命令: 看到: 总之,模 ...
- java 用RGB生成图片动态命名
import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java. ...
- [ZJU 1004] Anagrams by Stack
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- Spring定时器Quartz
<bean id="startQuertz" lazy-init="false" autowire="no" class=" ...
- WWDC2014代码和视频下载
WWDC2014 sample code 地址 http://pan.baidu.com/s/1qWGznnY WWDC2014 videos 地址 https://github.com/liubin ...
- 力扣60——第k个排列
原题 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: 1. "123" 2. &qu ...
- np.asarray(a, dtype=None, order=None)
np.asarray(a, dtype=None, order=None) 参数a:可以是,列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组 参数dtype=None, order=N ...