C#中反射和扩展方法如何运用
前段时间做了一个练手的小项目,名叫Book_Bar,用来卖书的,采用的是三层架构,也就是Models,IDAL,DAL,BLL和Web,在DAL层中各个类中有一个方法比较常用,那就是RowToClass,顾名思义,也就是将DataTable中的数据封装到Models中。结果导致在DAL各个类中写了很多类似的方法,后来就直接把它抽取出来做成了DataTable和DataRow的扩展方法,下面是代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data;
usingSystem.Reflection;
namespaceDAL
{
///
///用于给DataTable和DataRow扩展方法
///
publicstaticclassTableExtensionMethod
{
///
///功能:
///给DataTable扩展了一个方法,能够将DataTable中的行转变为对应的class对象,并封装到List集合中;
///
///需要转变成为的class类型
///传入的DataTable对象
///返回一个封装了对应class的List集合
publicstaticListTableToClass(thisDataTabletable)
{
Typetype=typeof(T);
PropertyInfo[]propArr=type.GetProperties();//获取所有属性
Listlist=newList();
DataRowCollectionrows=table.Rows;
intlen=rows[0].ItemArray.Length;//获取第一行的列数,即class的属性个数
for(inti=0;i
///功能:
///DataRow的扩展方法;
///能够将DataRow对象封装到泛型对象中
///
///需要转换成为的class类型
///被转换的行
///封装了行数据的class对象
publicstaticTRowToClass(thisDataRowrow)
{
//Typetype=Assembly.Load(classFullName).GetType();
Typetype=typeof(T);
Tt=(T)Activator.CreateInstance(type);
PropertyInfo[]propArr=type.GetProperties();
intlen=row.ItemArray.Length;
for(inti=0;i
///功能:
///DataRowCollection的扩展方法;
///能够将DataRowCollection对象封装到泛型List集合中
///
///
///
///
publicstaticListRowToClass(thisDataRowrow,DataRow[]rowArr)
{
Typetype=typeof(T);
PropertyInfo[]propArr=type.GetProperties();
intlen=rowArr[0].ItemArray.Length;//获取数据表第一行的列数,即属性个数
Listlist=newList();
for(inti=0;i
上面用到了泛型,反射,扩展方法。
之前在使用这行代码时出了点小问题:
propArr[i].SetValue(t,row[i]);
报了一个类型转换异常,断点调试之后发现是因为Models中的属性的排列和数据表的列的顺序不一样导致的,参照数据表中字段的顺序修改过来就好,还有一点就是在循环对属性进行赋值时,我选用的是数据表中列的个数,而不是属性的个数,(也就是代码中这里之所以不使用propArr.Length,是因为有些Models的属性在数据表中不存在对应的列
)。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。