I did a small practice project some time ago, called Book_Bar. It is used to sell books. It uses a three-layer architecture, namely Models, IDAL, DAL, BLL and Web. There is a method commonly used in each class in the DAL layer, that is, RowToClass, as the name implies, that is, encapsulate data in DataTable into Models. As a result, many similar methods were written in various DAL classes. Later, they were directly extracted and expanded into DataTable and DataRow. The following is the code:
using System; using ; using ; using ; namespace DAL { /// <summary> /// Used to extend the method to DataTable and DataRow /// </summary> public static class TableExtensionMethod { /// <summary> /// Function: /// Extended a method for DataTable, which can convert rows in DataTable into corresponding class objects and encapsulate them into List collection; /// </summary> /// <typeparam name="T">class type that needs to be converted to</typeparam> /// <param name="table">Incoming DataTable object</param> /// <returns>Returns a List collection encapsulating the corresponding class</returns> public static List<T> TableToClass<T>(this DataTable table) { Type type = typeof(T); PropertyInfo[] propArr = ();//Get all attributes List<T> list = new List<T>(); DataRowCollection rows = ; int len = rows[0].;//Get the number of columns in the first row, that is, the number of attributes of class for (int i = 0; i < ; i++) { T t = (T)(type); for (int j = 0; j < len; j++)//The reason why it is not used here is because some Models properties do not have corresponding columns in the data table. { propArr[j].SetValue(t, rows[i][j]); } (t); t = default(T); } return list; } /// <summary> /// Function: /// DataRow extension method; /// Ability to encapsulate DataRow objects into generic objects /// </summary> /// <typeparam name="T">class type that needs to be converted to</typeparam> /// <param name="row">Converted row</param> /// <returns> class object encapsulating row data</returns> public static T RowToClass<T>(this DataRow row) { //Type type = (classFullName).GetType(); Type type = typeof(T); T t = (T)(type); PropertyInfo[] propArr = (); int len = ; for (int i = 0; i < len; i++) { propArr[i].SetValue(t, row[i]); } return t; } /// <summary> /// Function: /// Extended method of DataRowCollection; /// Ability to encapsulate DataRowCollection objects into generic List collections /// </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 = (); int len = rowArr[0].;//Get the number of columns in the first row of the data table, that is, the number of attributes List<T> list = new List<T>(); for (int i = 0; i < ; i++) { T t = (T)(type); for (int j = 0; j < len; j++) { propArr[j].SetValue(t, rowArr[i][j]); } (t); t = default(T); } return list; } } }
Generics, reflection, and extension methods are used above.
Something went wrong when using this line of code:
propArr[i].SetValue(t, row[i]);
A type conversion exception was reported. After debugging the breakpoint, it was found that it was because the arrangement of attributes in Models and the order of columns in the data table was different. It was just modified according to the order of fields in the data table. Another point is that when assigning attributes in the loop, I chose the number of columns in the data table, not the number of attributes (that is, in the codeThe reason why it is not used here is that some Models properties do not have corresponding columns in the data table.
)。
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.