How to Use Reflection and Expansion Method in C

  • 2021-12-13 16:46:53
  • OfStack

Some time ago, I did a small project called Book_Bar, which was used to sell books. It adopted a three-tier architecture, namely Models, IDAL, DAL, BLL and Web. One method commonly used in each class in DAL layer is RowToClass, which, as its name implies, encapsulates the data in DataTable into Models. As a result, many similar methods were written in various classes of DAL, and then it was directly extracted into extended methods of DataTable and DataRow. Here is the code:


using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;


namespace DAL
{
 /// <summary>
 ///  Used for giving  DataTable And  DataRow Extension method 
 /// </summary>
 public static class TableExtensionMethod
 {

  /// <summary>
  ///  Functions: 
  ///   To DataTable Extended 1 Methods that can set the DataTable Converts the row in to the corresponding class Object and encapsulate it into the List In the collection; 
  /// </summary>
  /// <typeparam name="T"> What needs to be transformed into class Type </typeparam>
  /// <param name="table"> Incoming DataTable Object </param>
  /// <returns> Return 1 Encapsulates the corresponding class Adj. List Set </returns>
  public static List<T> TableToClass<T>(this DataTable table)
  {
   Type type = typeof(T);
   PropertyInfo[] propArr = type.GetProperties();// Get all properties 
   List<T> list = new List<T>();
   DataRowCollection rows = table.Rows;
   int len = rows[0].ItemArray.Length;// Get the 1 The number of columns in the row, that is class Number of attributes of 
   for (int i = 0; i < rows.Count; i++)
   {
    T t = (T)Activator.CreateInstance(type);
    for (int j = 0; j < len; j++)// The reason why it is not used here propArr.Length , because there are some Models The property of does not have a corresponding column in the data table 
    {
     propArr[j].SetValue(t, rows[i][j]);
    }
    list.Add(t);
    t = default(T);
   }
   return list;
  }

  /// <summary>
  ///  Functions: 
  ///  DataRow Extension method of; 
  ///   Be able to put DataRow Object is encapsulated in a generic object 
  /// </summary>
  /// <typeparam name="T"> To be converted to class Type </typeparam>
  /// <param name="row"> Converted rows </param>
  /// <returns> Object that encapsulates row data class Object </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>
  ///  Functions: 
  ///  DataRowCollection Extension method of; 
  ///   Be able to put DataRowCollection Object to a generic List In the collection 
  /// </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;// Get the data table 1 The number of columns in a row, that is, the number of attributes 
   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;
  }
 }
}

The above uses generics, reflection and extension methods.

There was a little problem using this line of code before:

propArr[i].SetValue(t, row[i]);

A type conversion exception was reported, After debugging breakpoints, it is found that the arrangement of attributes in Models and the order of columns in the data table are not 1. It is good to refer to the order of fields in the data table. Another point is that I choose the number of columns in the data table instead of the number of attributes when assigning values to attributes in a loop. (That is, the reason why propArr. Length is not used here in the code is that some Models attributes do not have corresponding columns in the data table
).


Related articles: