How can I convert a able to a list <t>?

Source: Internet
Author: User

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
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. Data;
  6. Using system. collections;
  7. Using system. reflection;
  8. Namespace databletolist
  9. {
  10. Class converthelper <t> where T: New ()
  11. {
  12. /// <Summary>
  13. /// Use reflection and generics
  14. /// </Summary>
  15. /// <Param name = "DT"> </param>
  16. /// <Returns> </returns>
  17. Public static list <t> converttolist (datatable DT)
  18. {
  19. // Define a set
  20. List <t> TS = new list <t> ();
  21. // Obtain the model type
  22. Type type = typeof (t );
  23. // Define a Temporary Variable
  24. String tempname = string. empty;
  25. // Traverse all data rows in the datatable
  26. Foreach (datarow DR in DT. Rows)
  27. {
  28. T = new T ();
  29. // Obtain the public attributes of this model
  30. Propertyinfo [] propertys = T. GetType (). getproperties ();
  31. // Traverse all attributes of the object
  32. Foreach (propertyinfo PI in propertys)
  33. {
  34. Tempname = pi. Name; // assign the attribute name to the Temporary Variable
  35. // Check whether the datatable contains this column (column name = Object attribute name)
  36. If (Dt. Columns. Contains (tempname ))
  37. {
  38. // Determine whether the property has a setter
  39. If (! Pi. canwrite) continue; // This attribute cannot be written and jumps out directly.
  40. // Value
  41. Object value = Dr [tempname];
  42. // If it is not empty, the property assigned to the object
  43. If (value! = Dbnull. value)
  44. Pi. setvalue (T, value, null );
  45. }
  46. }
  47. // Add an object to a generic set
  48. TS. Add (t );
  49. }
  50. Return ts;
  51. }
  52. }
  53. }


The example called in the main method is as follows:

 

 

[CSHARP]View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. Data;
  6. Using system. collections;
  7. Using system. reflection;
  8. Namespace databletolist
  9. {
  10. Class Program
  11. {
  12. Static void main (string [] ARGs)
  13. {
  14. Datatable dt = createdt (); // obtain a datatable
  15. // Obtain the generic set based on the object type and datatable
  16. List <person> List = converthelper <person>. converttolist (DT );
  17. // Print the output by traversing the generic set.
  18. Foreach (VAR item in List)
  19. {
  20. Console. writeline (item. tostring ());
  21. }
  22. Console. readkey ();
  23. }
  24. /// <Summary>
  25. /// Create a able and add data for testing.
  26. /// </Summary>
  27. /// <Returns> </returns>
  28. Public static datatable createdt ()
  29. {
  30. Datatable dt = new datatable ();
  31. DT. Columns. Add (New datacolumn ("ID", typeof (system. int32 )));
  32. DT. Columns. Add (New datacolumn ("name", typeof (system. String )));
  33. DT. Columns. Add (New datacolumn ("Address", typeof (system. String )));
  34. DT. Rows. Add (1, "Dylan", "SZ ");
  35. DT. Rows. Add (2, "Jay", "tw ");
  36. DT. Rows. Add (3, "CQ", "HK ");
  37. Return DT;
  38. }
  39. }
  40. }


The following is the code of a simple custom class:

 

 

[CSHARP]View plaincopy
    1. Using system;
    2. Using system. Collections. Generic;
    3. Using system. LINQ;
    4. Using system. text;
    5. Namespace databletolist
    6. {
    7. Class person
    8. {
    9. Private int ID;
    10. Public int ID
    11. {
    12. Get {return ID ;}
    13. Set {id = value ;}
    14. }
    15. Private string name;
    16. Public string name
    17. {
    18. Get {return name ;}
    19. Set {name = value ;}
    20. }
    21. Private string address;
    22. Public String address
    23. {
    24. Get {return address ;}
    25. Set {address = value ;}
    26. }
    27. Public override string tostring ()
    28. {
    29. Return "Person:" + ID + "," + name + "," + address;
    30. }
    31. }
    32. }

How can I convert a able to a list <t>?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.