A few days ago, I encountered a problem at work: I need to convert the queryed DataTable data source into a generic set of List<T> (known T type). The first reaction is that I think I must use "generics" (Isn't this nonsense? I said that I need to convert it into a List<T> generic collection), and I also need to use "reflection"-related things. hehe. Soon, I made a small example and passed the test. I will post the code below and share it with you. The code has detailed annotations, so readers can clearly understand my ideas.
First of all, this is a general conversion class I wrote to complete this kind of operation. It is also the most core part of implementing this function:
using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace DatableToList
{
class ConvertHelper<T> where T : new()
{
/// <summary>
///Use reflection and generics
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToList(DataTable dt)
{
// Define the collection
List<T> ts = new List<T>();
// Obtain the type of this model
Type type = typeof(T);
//Define a temporary variable
string tempName = ;
//Transfer all data rows in DataTable
foreach (DataRow dr in )
{
T t = new T();
// Obtain the public properties of this model
PropertyInfo[] propertys = ().GetProperties();
//Transfer all properties of this object
foreach (PropertyInfo pi in propertys)
{
tempName = ;// Assign the attribute name to the temporary variable
//Check whether DataTable contains this column (column name == object's attribute name)
if ((tempName))
{
// Determine whether this property has a Setter
If (!) continue;//This property cannot be written, it will jump out directly
//Get the value
object value = dr[tempName];
//If it is not empty, then the attribute assigned to the object
if (value != )
(t, value, null);
}
}
//Add objects to generic collection
(t);
}
return ts;
}
}
}
Below is an example called in the Main method:
using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace DatableToList
{
class Program
{
static void Main(string[] args)
{
DataTable dt = CreateDt();//Get a DataTable
//Get generic collections based on object type and DataTable
List<Person> list = ConvertHelper<Person>.ConvertToList(dt);
//Transf the generic collection and print out.
foreach (var item in list)
{
(());
}
();
}
/// <summary>
/// Create a DataTable, add data, and provide tests.
/// </summary>
/// <returns></returns>
public static DataTable CreateDt()
{
DataTable dt = new DataTable();
(new DataColumn("id", typeof(System.Int32)));
(new DataColumn("name", typeof()));
(new DataColumn("address", typeof()));
(1,"Dylan","SZ");
(2, "Jay", "TW");
(3, "CQ", "HK");
return dt;
}
}
}
At the bottom is the code for a custom simple class:
using System;
using ;
using ;
using ;
namespace DatableToList
{
class Person
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
public override string ToString()
{
return "Person: " + id + " ," + name+","+address;
}
}
}
The above example test has been passed, and the level is limited, so please apologize for the shortcomings.