Convert from datarow to an object

Source: Internet
Author: User

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Datatable TB = new datatable ();
Datacolumn C1 = new datacolumn ("ID ");
Datacolumn C2 = new datacolumn ("name ");
Datacolumn C3 = new datacolumn ("sex ");
TB. Columns. Add (C1 );
TB. Columns. Add (C2 );
TB. Columns. Add (C3 );
Datarow DR = Tb. newrow ();
Dr ["ID"] = "1 ";
Dr ["name"] = "halfcup ";
Dr ["sex"] = "female ";
TB. Rows. Add (DR );
Class1 OBJ = new class1 ();
Copydatarow2object (DR, OBJ );
}

 

/// <Summary>
/// Generate OBJ from datarow
/// </Summary>
/// <Param name = "Dr"> original data datarow </param>
/// <Param name = "OBJ"> object for receiving data </param>
Public static void copydatarow2object (datarow DR, object OBJ)
{
Type type = obj. GetType ();
Fieldinfo [] aryfieldinfo = type. getfields (); // obtain all public fields of the current type
Propertyinfo [] arypropertyinfo = type. getproperties (); // obtain all public properties of the current type
String Cols = getcolunms (DR); // obtain all columns of datarow, such as ID, name, sex,
Cols = "," + Cols; //, ID, name, sex,
Foreach (fieldinfo fi in aryfieldinfo)
{
String proname = getpropertyname (Fi. Name); // fi. Name field name
String S = "," + proname + ",";
If (cols. indexof (S) =-1) continue;
If (Dr [proname]. tostring (). Equals ("")&&! Fi. fieldtype. Equals (typeof (string) continue;
Fi. setvalue (OBJ, convert. changetype (Dr [proname]. tostring (), Fi. fieldtype); // set the field value
}
Foreach (propertyinfo PI in arypropertyinfo)
{
String proname = getpropertyname (PI. Name); // pi. Name is ID and proname is ID
String S = "," + proname + ",";
If (cols. indexof (S) =-1) continue;
If (Dr [proname]. tostring (). Equals ("")&&! Pi. propertytype. Equals (typeof (string) continue; // Dr ["ID"] if it is null and its attribute type is string
Pi. setvalue (OBJ, convert. changetype (Dr [proname]. tostring (), Pi. propertytype), null); // set the attribute value (object, attribute value, whether to index the attribute value)
// Convert. changetype (object, type) indicates the object whose type is type and whose value is object.
}
}

///


// obtain the attribute name
///
/// original name
// name after processing
Private Static string getpropertyname (string name)
{< br> string S = Name. toupper ();
If (S. indexof ("set") = 0)
{< br> S = S. substring (3);
}< br> return s;
}

/// <Summary>
/// Obtain all column names in the table
/// </Summary>
/// <Param name = "Dr"> one datarow </param>
/// <Returns> return all column names </returns>
Private Static string getcolunms (datarow Dr)
{
Stringbuilder sb = new stringbuilder ();
Datacolumncollection DCC = dr. Table. columns;
For (INT I = 0; I <DCC. Count; I ++)
{
SB. appendformat ("{0},", DCC [I]. tostring ());
}
Return sb. tostring ();
}
}

 

 

 

The following is an object class:

Public class class1
{
Public class1 ()
{
//
// Todo: add the constructor logic here
//
}
Private int ID;

Public int ID
{
Get {return ID ;}
Set {id = value ;}
}
Private string name;

Public string name
{
Get {return name ;}
Set {name = value ;}
}
Private string sex;

Public String sex
{< br> get {return sex ;}< br> set {sex = value ;}
}< BR >}

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.