C#中反射和擴展方法如何運用
前段時間做了一個練手的小項目,名叫Book_Bar,用來賣書的,采用的是三層架構(gòu),也就是Models,IDAL,DAL,BLL 和 Web , 在DAL層中各個類中有一個方法比較常用,那就是RowToClass ,顧名思義,也就是將DataTable 中的數(shù)據(jù)封裝到Models 中。結(jié)果導(dǎo)致在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擴展了一個方法,能夠?qū)ataTable中的行轉(zhuǎn)變?yōu)閷?yīng)的class對象,并封裝到List集合中; /// </summary> /// <typeparam name="T">需要轉(zhuǎn)變成為的class類型</typeparam> /// <param name="table">傳入的DataTable對象</param> /// <returns>返回一個封裝了對應(yīng)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;//獲取第一行的列數(shù),即class的屬性個數(shù) for (int i = 0; i < rows.Count; i++) { T t = (T)Activator.CreateInstance(type); for (int j = 0; j < len; j++)//這里之所以不使用propArr.Length,是因為有些Models的屬性在數(shù)據(jù)表中不存在對應(yīng)的列 { propArr[j].SetValue(t, rows[i][j]); } list.Add(t); t = default(T); } return list; } /// <summary> /// 功能: /// DataRow的擴展方法; /// 能夠?qū)ataRow對象封裝到泛型對象中 /// </summary> /// <typeparam name="T">需要轉(zhuǎn)換成為的class類型</typeparam> /// <param name="row">被轉(zhuǎn)換的行</param> /// <returns>封裝了行數(shù)據(jù)的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的擴展方法; /// 能夠?qū)ataRowCollection對象封裝到泛型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;//獲取數(shù)據(jù)表第一行的列數(shù),即屬性個數(shù) 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]);
報了一個類型轉(zhuǎn)換異常,斷點調(diào)試之后發(fā)現(xiàn)是因為 Models 中的屬性的排列和數(shù)據(jù)表的列的順序不一樣導(dǎo)致的,參照數(shù)據(jù)表中字段的順序修改過來就好,還有一點就是在循環(huán)對屬性進行賦值時,我選用的是數(shù)據(jù)表中列的個數(shù),而不是屬性的個數(shù),(也就是代碼中這里之所以不使用propArr.Length,是因為有些Models的屬性在數(shù)據(jù)表中不存在對應(yīng)的列
)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法示例
欄 目:C#教程
下一篇:C#使用NPOI上傳excel
本文標(biāo)題:C#中反射和擴展方法如何運用
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5740.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
- 01-10C#中DataGridView常用操作實例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#利用反射技術(shù)實現(xiàn)去掉按鈕選中時的邊框效果
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#及WPF獲取本機所有字體和顏色的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文