DataTable and List & lt; T & gt; Conversion Using Reflection

Source: Internet
Author: User

Today, I am not very busy at work. I want to summarize reflection, extension methods, and lambda expression usage. I wrote a small DEMO record myself, I hope that you will not find any mistakes right away. In this article, I will only describe my usage. As a memo, I will not explain the basic syntax. If you are not familiar with these concepts, search for them on the blog, I found a lot of related explanations, and the younger brother gave me some advice.

Additional Links: If you want to know the basic knowledge of c #1.0 -- c #4.0, click the following link. You can also see the knowledge points mentioned in this article.

Click me

  • Through reflection, we can provide type information so that our developers can use this information to construct and use objects at runtime.
  • Reflection allows programs to dynamically add various features during execution.

For more information, see my 163 blog. NET reflection.

Extension Method

Introduce MSDN's definition of extension methods: extension methods enable you to "add" methods to existing types (including your custom types and objects ).Instead of creating a new derived type, re-compiling, or modifying the original type in other ways. An extension method is a special static method, but it can be called like an instance method of an extension type. For client code written in C #, there is no significant difference between calling an extension method and calling a method actually defined in the type.

Lambda expressions

Baidu encyclopedia defines Lambda as an anonymous function. It can contain expressions and statements and can be used to create delegate or expression directory tree types. All Lambda expressions use the Lambda operator =>, which reads "goes ". The left side of the Lambda operator is the input parameter (if any), and the right side contains the expression or statement block.

After talking about this, start the code.

Method 1: DataTable to List using extension and reflection <T>

   List<T> ToList<T>( DataTable dt)  T:,==<T> lst =  List<T> typeName =  (DataRow dr =  (PropertyInfo pi = (!pi.CanWrite)  value = (value == DBNull.Value)  (pi.PropertyType == (  (pi.PropertyType == () || pi.PropertyType == (?.Parse(value.ToString()),   (pi.PropertyType == (DateTime?) || pi.PropertyType ==   (pi.PropertyType == (.Parse(value.ToString()),   (pi.PropertyType == (.Parse(value.ToString()), 

Method 1 call

(1) first create an object class

    Name { ;   Age{;

DataTable dt = =] = ] = =] = ] = <People> p = dt.ToList<People>();

Method 2: Extended method and Action delegate implement array traversal (ForEach)

   ForEach<T>( IEnumerable<T> ien,Action<T> ( item 

 List<> lst =  List<> arr =  [] { , , => lst.Add(it));        Response.Write(lst[]);

 

  DataTable ToDataTable<T>( IEnumerable<T> value)  T : ,<PropertyInfo> lstProperty =  List<PropertyInfo>== =>   (p.PropertyType.IsGenericType) (value !=  ( item                    DataRow row ==>= p.GetValue(item, 

Okay, so it's time to go to bed today. Learn a little bit every day and make a little progress every day.

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.