C#中反射和扩展方法如何运用
作者:码上猿梦 时间:2023-08-02 01:43:16
前段时间做了一个练手的小项目,名叫Book_Bar,用来卖书的,采用的是三层架构,也就是Models,IDAL,DAL,BLL 和 Web , 在DAL层中各个类中有一个方法比较常用,那就是RowToClass ,顾名思义,也就是将DataTable 中的数据封装到Models 中。结果导致在DAL各个类中写了很多类似的方法,后来就直接把它抽取出来做成了DataTable和DataRow的扩展方法,下面是代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace DAL
{
/// <summary>
/// 用于给 DataTable和 DataRow扩展方法
/// </summary>
public static class TableExtensionMethod
{
/// <summary>
/// 功能:
/// 给DataTable扩展了一个方法,能够将DataTable中的行转变为对应的class对象,并封装到List集合中;
/// </summary>
/// <typeparam name="T">需要转变成为的class类型</typeparam>
/// <param name="table">传入的DataTable对象</param>
/// <returns>返回一个封装了对应class的List集合</returns>
public static List<T> TableToClass<T>(this DataTable table)
{
Type type = typeof(T);
PropertyInfo[] propArr = type.GetProperties();//获取所有属性
List<T> list = new List<T>();
DataRowCollection rows = table.Rows;
int len = rows[0].ItemArray.Length;//获取第一行的列数,即class的属性个数
for (int i = 0; i < rows.Count; i++)
{
T t = (T)Activator.CreateInstance(type);
for (int j = 0; j < len; j++)//这里之所以不使用propArr.Length,是因为有些Models的属性在数据表中不存在对应的列
{
propArr[j].SetValue(t, rows[i][j]);
}
list.Add(t);
t = default(T);
}
return list;
}
/// <summary>
/// 功能:
/// DataRow的扩展方法;
/// 能够将DataRow对象封装到泛型对象中
/// </summary>
/// <typeparam name="T">需要转换成为的class类型</typeparam>
/// <param name="row">被转换的行</param>
/// <returns>封装了行数据的class对象</returns>
public static T RowToClass<T>(this DataRow row)
{
//Type type = Assembly.Load(classFullName).GetType();
Type type = typeof(T);
T t = (T)Activator.CreateInstance(type);
PropertyInfo[] propArr = type.GetProperties();
int len = row.ItemArray.Length;
for (int i = 0; i < len; i++)
{
propArr[i].SetValue(t, row[i]);
}
return t;
}
/// <summary>
/// 功能:
/// DataRowCollection的扩展方法;
/// 能够将DataRowCollection对象封装到泛型List集合中
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="rows"></param>
/// <returns></returns>
public static List<T> RowToClass<T>(this DataRow row, DataRow[] rowArr)
{
Type type = typeof(T);
PropertyInfo[] propArr = type.GetProperties();
int len = rowArr[0].ItemArray.Length;//获取数据表第一行的列数,即属性个数
List<T> list = new List<T>();
for (int i = 0; i < rowArr.Length; i++)
{
T t = (T)Activator.CreateInstance(type);
for (int j = 0; j < len; j++)
{
propArr[j].SetValue(t, rowArr[i][j]);
}
list.Add(t);
t = default(T);
}
return list;
}
}
}
上面用到了泛型,反射,扩展方法。
之前在使用这行代码时出了点小问题:
propArr[i].SetValue(t, row[i]);
报了一个类型转换异常,断点调试之后发现是因为 Models 中的属性的排列和数据表的列的顺序不一样导致的,参照数据表中字段的顺序修改过来就好,还有一点就是在循环对属性进行赋值时,我选用的是数据表中列的个数,而不是属性的个数,(也就是代码中这里之所以不使用propArr.Length,是因为有些Models的属性在数据表中不存在对应的列
)。
来源:http://www.cnblogs.com/daimajun/p/6818069.html
标签:C#,反射,扩展
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#弹出对话框确定或者取消执行相应操作的实例代码
2021-11-19 13:26:29
C语言内存操作函数详解
2021-09-01 01:50:36
![](https://img.aspxhome.com/file/2023/4/114934_0s.png)
SpringBoot整合java诊断工具Arthas解读
2023-08-07 10:39:03
![](https://img.aspxhome.com/file/2023/0/69920_0s.png)
Java Spring5学习之JdbcTemplate详解
2023-11-25 20:17:23
![](https://img.aspxhome.com/file/2023/2/60412_0s.png)
浅谈Java安全之C3P0链利用与分析
2022-05-23 20:31:17
![](https://img.aspxhome.com/file/2023/0/95950_0s.jpg)
一天时间用Java写了个飞机大战游戏,朋友直呼高手
2023-12-11 10:51:30
![](https://img.aspxhome.com/file/2023/2/81892_0s.png)
Spring Boot Redis 集成配置详解
2022-12-05 20:57:59
详解C#中使用对象或集合的初始值设定项初始化的操作
2021-12-25 20:04:52
c#基数排序Radix sort的实现方法
2021-07-25 02:02:21
Java基础知识之ByteArrayInputStream流的使用
2023-10-27 14:37:53
HighCharts图表控件在ASP.NET WebForm中的使用总结(全)
2022-07-13 02:11:12
![](https://img.aspxhome.com/file/2023/9/108849_0s.png)
部署Java在服务器端的EJB组件的方法
2023-08-26 13:43:38
SpringBoot 如何优雅的实现跨服务器上传文件的示例
2023-07-03 21:42:56
![](https://img.aspxhome.com/file/2023/9/83039_0s.png)
SpringCloud让微服务实现指定程序调用
2022-03-05 23:24:15
![](https://img.aspxhome.com/file/2023/2/88552_0s.png)
Unity3D实现打砖块游戏
2021-11-13 11:29:44
![](https://img.aspxhome.com/file/2023/5/129735_0s.jpg)
Android自定义view实现有header和footer作为layout使用的滚动控件
2023-07-31 19:29:54
Android MediaPlayer实现音乐播放器实例代码
2021-05-26 22:58:05
Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)
2023-11-21 23:56:29
![](https://img.aspxhome.com/file/2023/8/103918_0s.gif)
C#实现自定义双击事件
2023-05-01 16:31:57
C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
2022-05-17 11:46:16
![](https://img.aspxhome.com/file/2023/2/88372_0s.png)