Method for converting a able into a class

Source: Internet
Author: User

/// <Summary>

/// Use the attribute name of the class to correspond to the field name in the DataTable

/// </Summary>

Public static class TableToModel

{

/// <Summary>

/// DataRow Extension Method: converts the DataRow type to an object of the specified type.

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Returns> </returns>

Public static T ToModel <T> (this DataRow dr) where T: class, new ()

{

Return ToModel <T> (dr, true );

}

/// <Summary>

/// Convert the DataRow type to an object of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Param name = "dateTimeToString"> whether to convert a date to a string. The default value is true. </param>

/// <Returns> </returns>

/// <Summary>

Public static T ToModel <T> (this DataRow dr, bool dateTimeToString) where T: class, new ()

{

If (dr! = Null)

Return ToList <T> (dr. Table, dateTimeToString). First ();

 

Return null;

}

 

/// <Summary>

/// Convert the DataTable type to an object set of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Returns> </returns>

Public static List <T> ToList <T> (this DataTable dt) where T: class, new ()

{

Return ToList <T> (dt, true );

}

 

/// <Summary>

/// Convert the DataTable type to an object set of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Param name = "dateTimeToString"> whether to convert a date to a string. The default value is true. </param>

/// <Returns> </returns>

Public static List <T> ToList <T> (this DataTable dt, bool dateTimeToString) where T: class, new ()

{

List <T> list = new List <T> ();

 

If (dt! = Null)

{

List <PropertyInfo> infos = new List <PropertyInfo> ();

 

Array. ForEach <PropertyInfo> (typeof (T). GetProperties (), p =>

{

If (Dt. Columns. Contains (P. Name) = true)

{

Infos. Add (P );

}

});

 

Setlist <t> (list, Infos, DT, datetimetostring );

}

 

Return list;

}

 

# Region private Method

 

Private Static void setlist <t> (list <t> list, list <propertyinfo> Infos, datatable DT, bool datetimetostring) where T: Class, new ()

{

Foreach (datarow DR in DT. Rows)

{

T model = new T ();

 

Infos. ForEach (p =>

{

If (dr [p. Name]! = DBNull. Value)

{

Object tempValue = dr [p. Name];

If (dr [p. Name]. GetType () = typeof (DateTime) & dateTimeToString = true)

{

TempValue = dr [p. Name]. ToString ();

}

Try

{

P. SetValue (model, tempValue, null );

}

Catch {}

}

});

List. Add (model );

}

}

 

# Endregion

}

 

/// <Summary>

/// Use the Description attribute of the class attribute to correspond to the field name in the DataTable

/// </Summary>

Public static class TableToModel

{

/// <Summary>

/// Convert the DataRow type to an object of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Returns> </returns>

Public static T ToModel <T> (this DataRow dr) where T: class, new ()

{

Return ToModel <T> (dr, true );

}

/// <Summary>

/// Convert the DataRow type to an object of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Param name = "dateTimeToString"> whether to convert a date to a string. The default value is true. </param>

/// <Returns> </returns>

/// <Summary>

Public static T ToModel <T> (this DataRow dr, bool dateTimeToString) where T: class, new ()

{

If (dr! = Null)

Return ToList <T> (dr. Table, dateTimeToString). First ();

 

Return null;

}

 

/// <Summary>

/// Convert the DataTable type to an object set of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Returns> </returns>

Public static List <T> ToList <T> (this DataTable dt) where T: class, new ()

{

Return ToList <T> (dt, true );

}

 

/// <Summary>

/// Convert the DataTable type to an object set of the specified type

/// </Summary>

/// <Typeparam name = "T"> entity type </typeparam>

/// <Param name = "datetimetostring"> whether to convert a date to a string. The default value is true. </param>

/// <Returns> </returns>

Public static list <t> tolist <t> (this datatable DT, bool datetimetostring) where T: Class, new ()

{

List <t> List = new list <t> ();

 

If (DT! = NULL)

{

List <PropertyInfo> infos = new List <PropertyInfo> ();

 

Array. ForEach <PropertyInfo> (typeof (T). GetProperties (), p =>

{

Foreach (Attribute att in p. GetCustomAttributes (true ))

{

System. ComponentModel. DescriptionAttribute dscript = att as System. ComponentModel. DescriptionAttribute;

If (dscript! = Null)

{

If (dt. Columns. Contains (dscript. Description) = true)

{

Infos. Add (p );

}

}

}

 

});

 

SetList <T> (list, infos, dt, dateTimeToString );

}

 

Return list;

}

 

# Region private Method

 

Private static void SetList <T> (List <T> list, List <PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T: class, new ()

{

Foreach (DataRow dr in dt. Rows)

{

T model = new T ();

 

Infos. ForEach (p =>

{

Foreach (Attribute att in p. GetCustomAttributes (true ))

{

System. componentmodel. descriptionattribute dscript = ATT as system. componentmodel. descriptionattribute;

If (dscript! = NULL)

{

If (Dr [dscript. description]! = Dbnull. value)

{

Object tempValue = dr [dscript. Description];

If (dr [dscript. Description]. GetType () = typeof (DateTime) & dateTimeToString = true)

{

TempValue = dr [p. Name]. ToString ();

}

Try

{

P. SetValue (model, tempValue, null );

}

Catch {}

}

}

}

});

List. Add (model );

}

}

 

# Endregion

}

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.