Converts a able to a List, and converts a List to an implementation class of the DataTable.

Source: Internet
Author: User

Converts a able to a List, and converts a List to an implementation class of the DataTable.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. linq; using System. reflection; using System. text; using System. threading. tasks; namespace Xmh. DBUnit {/// <summary> /// converts a DataTable to List, convert List to DataTable implementation class // </summary> public static class DataTableHelper {public static DataTable convertize <T> (IList <T> list) {DataTable table = CreateTable <T> (); Type entityType = typeof (T); PropertyDescriptorCollection properties = TypeDescriptor. getProperties (entityType); foreach (T item in list) {DataRow row = table. newRow (); foreach (PropertyDescriptor prop in properties) row [prop. name] = prop. getValue (item); table. rows. add (row) ;}return table ;}public static IList <T> convertize <T> (IList <DataRow> rows) {IList <T> list = null; if (rows! = Null) {list = new List <T> (); foreach (DataRow row in rows) {T item = CreateItem <T> (row); list. add (item) ;}}return list;} public static IList <T> convertize <T> (DataTable table) {if (table = null) return null; list <DataRow> rows = new List <DataRow> (); foreach (DataRow row in table. rows) rows. add (row); return convertize <T> (rows) ;}// Convert DataRow into T Object public static T CreateItem <T> (Da TaRow row) {string columnName; T obj = default (T); if (row! = Null) {obj = Activator. createInstance <T> (); foreach (DataColumn column in row. table. columns) {columnName = column. columnName; // Get property with same columnName PropertyInfo prop = obj. getType (). getProperty (columnName); try {Type = prop. propertyType; // determines whether the type is generic, because nullable is a generic class, if (type. isGenericType & type. getGenericTypeDefinition (). equals (typeof (Nullable <>) // determines whether convertsionType is Nullable generic class {// if the type is nullable class, declare an NullableConverter class, which provides the conversion from the Nullable class to the basic primitive type System. componentModel. nullableConverter nullableConverter = new System. componentModel. nullableConverter (type); // converts type to nullable's basic primitive type = nullableConverter. underlyingType;} // Get value for the column object value = (row [columnName]. getType () = typeof (DBNull ))? Null: row [columnName]; // Set property value if (prop. CanWrite) // determines whether it can be written to prop. SetValue (obj, (value = null? Null: Convert. changeType (value, type), null);} catch {throw; // Catch whatever here }}return obj;} public static DataTable CreateTable <T> () {Type entityType = typeof (T); DataTable table = new DataTable (entityType. name); PropertyDescriptorCollection properties = TypeDescriptor. getProperties (entityType); foreach (PropertyDescriptor prop in properties) table. columns. add (prop. name, prop. prope RtyType); return table;} public static List <T> ToList <T> (this DataTable dt) where T: class, new () {Type t = typeof (T ); propertyInfo [] propertys = t. getProperties (); List <T> lst = new List <T> (); string typeName = string. empty; foreach (DataRow dr in dt. rows) {T entity = new T (); foreach (PropertyInfo pi in propertys) {typeName = pi. name; if (dt. columns. contains (typeName) {if (! Pi. canWrite) continue; object value = dr [typeName]; if (value = DBNull. value) continue; if (pi. propertyType = typeof (string) {pi. setValue (entity, value. toString (), null);} else if (pi. propertyType = typeof (int) | pi. propertyType = typeof (int ?)) {Pi. SetValue (entity, int. Parse (value. ToString (), null);} else if (pi. PropertyType = typeof (DateTime ?) | Pi. propertyType = typeof (DateTime) {pi. setValue (entity, DateTime. parse (value. toString (), null);} else if (pi. propertyType = typeof (float) {pi. setValue (entity, float. parse (value. toString (), null);} else if (pi. propertyType = typeof (double) {pi. setValue (entity, double. parse (value. toString (), null);} else {pi. setValue (entity, value, null) ;}} lst. add (entity) ;}return lst ;}}}

 

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.