Use reflection to convert the data queried by the database to Model and List (improved version), and convert the data to model.

Source: Internet
Author: User

Use reflection to convert the data queried by the database to Model and List (improved version), and convert the data to model.

I have written such a blog post before, but it is very rough. Blog address

Then I came to the comments of a senior (@ diligent bear) on my blog, and showed me a more elegant implementation method, so I reconstructed the previous code.

 

1 public static List <T> ConvertToList <T> (DataTable dt) 2 {3 List <T> list = new List <T> (); // define the set 4 Type type = typeof (T); // obtain the model type 5 string tempName = ""; 6 PropertyInfo [] propertys = Type. getProperties (); // obtain the public attributes of this model 7 foreach (DataRow dr in dt. rows) 8 {9 // create a model 10 object obj = type. assembly. createInstance (type. fullName); 11 foreach (PropertyInfo pi in propertys) 12 {13 tempName = pi. nam E; 14 if (dt. Columns. Contains (tempName) 15 {16 if (! Pi. CanWrite) continue; 17 object value = dr [tempName]; 18 if (value! = DBNull. value) 19 pi. setValue (obj, value, null); 20} 21} 22 list. add (T) obj); 23} 24 return list; 25} 26 27 public static List <T> ConvertToList <T> (IDataReader reader) 28 {29 List <T> list = new List <T> (); // defines the set 30 Type type = typeof (T ); // obtain the model type 31 string tempName = ""; 32 PropertyInfo [] propertys = type. getProperties (); // get the public attributes of this model 33 while (reader. read () 34 {35 // create a model 36 object obj = Type. assembly. createInstance (type. fullName); 37 foreach (PropertyInfo pi in propertys) 38 {39 tempName = pi. name; 40 if (ReaderExists (reader, tempName) 41 {42 if (! Pi. CanWrite) continue; 43 object value = reader [tempName]; 44 if (value! = DBNull. value) 45 pi. setValue (obj, value, null); 46} 47} 48 list. add (T) obj); 49} 50 return list; 51} 52 53 public static T ConvertToModel <T> (IDataReader reader) 54 {55 Type type = typeof (T ); 56 PropertyInfo [] proList = type. getProperties (); 57 // create a model 58 object obj = type. assembly. createInstance (type. fullName); 59 string tempName = ""; 60 if (reader. read () 61 {62 foreach (PropertyInfo pi In proList) 63 {64 tempName = pi. Name; 65 if (ReaderExists (reader, pi. Name) 66 {67 if (! Pi. CanWrite) continue; 68 object value = reader [tempName]; 69 if (value! = DBNull. value) 70 pi. setValue (obj, value, null); 71} 72} 73} 74 return (T) obj; 75} 76 77 public static T ConvertToModel <T> (DataRow row) 78 {79 Type type = typeof (T); 80 PropertyInfo [] proList = type. getProperties (); 81 // create a model 82 object obj = type. assembly. createInstance (type. fullName); 83 string tempName = ""; 84 foreach (PropertyInfo pi in proList) 85 {86 tempName = pi. name; 87 if (! String. IsNullOrEmpty (row [tempName]. ToString () 88 {89 if (! Pi. CanWrite) continue; 90 object value = row [tempName]; 91 if (value! = DBNull. value) 92 pi. setValue (obj, value, null); 93} 94} 95 return (T) obj; 96} 97 98 // <summary> 99 // verify whether reader has a column 100 /// </summary> 101 // <param name = "reader"> </param> 102 // <param name = "columnName"> </param> 103 // <returns> </returns> 104 private static bool ReaderExists (IDataReader reader, string columnName) 105 {106 int count = reader. fieldCount; 107 for (int I = 0; I <count; I ++) 108 {109 if (reader. getName (I ). equals (columnName) 110 {111 return true; 112} 113 return false; 114}View Code

 

Related Article

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.