Yesterday at work, I encountered a problem: I needed to convert the queried datatable data source to a list <t> generic set (known as the T type ). First, I want to use "generic" (isn't that nonsense? All said to be converted to a list <t> generic set), and "reflection" is also used. Haha. Soon I made a small instance and passed the test. Below I will post the code and share it with you. The Code has detailed annotations, so readers can clearly understand my ideas.
First, this is a general conversion class I wrote to complete this operation. It is also the core part of this function:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data; using system. collections; using system. reflection; 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 set list <t> TS = new list <t> ();// Obtain the type = typeof (T) of this model; // define a temporary variable string tempname = string. empty; // traverses all data rows in the datatable foreach (datarow DR in DT. rows) {T = new T (); // obtain the public attributes of this model. propertyinfo [] propertys = T. getType (). getproperties (); // traverses all attributes of the object. foreach (propertyinfo PI in propertys) {tempname = pi. name; // assign the attribute name to the Temporary Variable // check whether the datatable contains this column (column name = Object attribute name) if (DT. columns. contains (tempname) {// determines whether this property has setter if (! Pi. canwrite) continue; // This attribute cannot be written, jump out directly // value object value = Dr [tempname]; // if it is not empty, the property if (value! = Dbnull. value) Pi. setvalue (T, value, null) ;}// Add the object to ts in the generic set. add (t) ;}return ts ;}}}
The example called in the main method is as follows:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data; using system. collections; using system. reflection; namespace databletolist {class program {static void main (string [] ARGs) {datatable dt = createdt (); // obtain a datatable // according to the object type and datatable, obtain the generic set list <person> List = converthelper <person>. converttolist (DT); // print the output by traversing the generic set. Foreach (VAR item in list) {console. writeline (item. tostring ();} console. readkey () ;}//< summary> /// create a able and add data for testing. /// </Summary> /// <returns> </returns> Public static datatable createdt () {datatable dt = new datatable (); DT. columns. add (New datacolumn ("ID", typeof (system. int32); DT. columns. add (New datacolumn ("name", typeof (system. string); DT. columns. add (New datacolumn ("Address", typeof (system. string); DT. rows. add (1, "Dylan", "SZ"); DT. rows. add (2, "Jay", "tw"); DT. rows. add (3, "CQ", "HK"); Return DT ;}}}
The following is the code of a simple custom class:
using System;using System.Collections.Generic;using System.Linq;using System.Text;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; } }}
We have passed the test in the above example. The level is limited. We apologize for any shortcomings.
This article is from blog.csdn.net/dinglang_2009.