From: http://blog.csdn.net/dinglang_2009/article/details/6951138
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:
[Java]View plaincopy
- 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 a set
- List <t> TS = new list <t> ();
- // Obtain the model type
- Type type = typeof (t );
- // Define a Temporary Variable
- String tempname = string. empty;
- // Traverse 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 ();
- // Traverse 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 ))
- {
- // Determine whether the property has a setter
- If (! Pi. canwrite) continue; // This attribute cannot be written and jumps out directly.
- // Value
- Object value = Dr [tempname];
- // If it is not empty, the property assigned to the object
- If (value! = Dbnull. value)
- Pi. setvalue (T, value, null );
- }
- }
- // Add an object to a generic set
- TS. Add (t );
- }
- Return ts;
- }
- }
- }
The example called in the main method is as follows:
[CSHARP]View plaincopy
- 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
- // Obtain the generic set based on the object type and datatable
- 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:
[CSHARP]View plaincopy
- 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;
- }
- }
- }
How can I convert a able to a list <t>?