General conversion between able and object set in C # (using the extension method)

Source: Internet
Author: User

In this case, DataRow is converted to a single entity, and dataTable is converted to a List generic type, which supports time format conversion.

The following methods are all extension methods. The extension method must be written in a static class, and the method should also be static.

  • Medium
  • (The first parameter type is also called the extension type, that is, the method is used to expand this type)

 

 

1. Convert DataRow to an object

                                      T ToModel<T>( DataRow dr)  T : , ToModel<T>(dr,


2. Convert DataRow to an object and set the time format conversion.

                                                                T ToModel<T>( DataRow dr,  dateTimeToString)  T : ,  (dr !=  ToList<T> 


3. convert a able to an object

                                                      List<T> ToList<T>( DataTable dt,  dateTimeToString)  T : , <T> list =  List<T> (dt != <PropertyInfo> infos =  List<PropertyInfo><PropertyInfo>((T).GetProperties(), p => (dt.Columns.Contains(p.Name) == <T>


4. Conversion implementation code

     SetList<T>(List<T> list, List<PropertyInfo> infos, DataTable dt,  dateTimeToString)  T : ,  (DataRow dr = => (dr[p.Name] != DBNull.Value) tempValue = (dr[p.Name].GetType() == (DateTime) && dateTimeToString == )=);        }

 

 

 

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.