C # simplifies assignment to class fields with reflection

Source: Internet
Author: User
Tags flock join reflection

The main idea of this example is to establish a class and database query statement of the field structure is consistent

Then using reflection, the data field names are pieced together and assigned to the fields of the class object.

1. Definition of class

Namespace Ccb_donet.classfolder
{
public class Fieldruleinfo
{
public string gstrfno;
Public Strin G Gstrfname;
public string Gstrflock;
public string gstrfcaption;
public string Gstrftype;
public string Gstrfmust;
public string gstrfmin;
public string Gstrfmax;
public string Gstrfdefault;
public string gstrfdate;
public string Gstrfdb;
public string Gstrfallow;
public string Gstrfdisallow;
public string GSTRFSB;
public string Gstrfbig;
public string Gstrfsmall;
public string Gstrfinputmethod;
public string Gstrfchk;
public string gstrfrelation;
public string Gstrfdesc;
public string Gstrfsecond;
public string Gstrfqc;
public string gstrfexception;
public string Gstrfasupp;
public string Gstrfyqh;
public string Gstrfpos;
public string Gstrfstar;
public string Gstrfsave;
public string gstrfaddress;
public string Gstrflblcolor;
public String GstrfischeCklist;
}
}

#region Load Field rules
private bool M_getrule ()
{
String strSQL = "";
DataTable dtget = null;

#if (DEBUG)
Try
{
#endif

if (Common.ginttypeorder = =)
{
strSQL = ' Select A.fno,a.fname,a.flock,a.fcaption,a.ftype, ' +
' a.fmust, A.fmin,a.fmax,a.fdefault,a.fdate,rn "+
" A.fdb,a.fallow,a.fdisallow,a.fsb,a.fbig,a.fsmall,a.finputmethod, "+
"A.fchk,a.frelation,a.fdesc,a.fsecond,rn" +
"A.fqc,a.fexception,a.fasupp,a.fyqh,a.fpos,a.fstar,a.fsave," +
' a.faddress,a.flblcolor,a.fischecklist from P_field_rule95 A rn ' +
' INNER JOIN p_field_initial B on A.fno=b.fno RN "+
" where a.formtype=1 and b.fsection= ' 1 ' and "+
" (b.fregion95=1 OR b.fregion95=-1) Order by A.forder;

Else
{
strSQL = ' Select A.fno,a.fname,a.flock,a.fcaption,a.ftype, ' +
' a.fmust,a.fmin,a.fmax,a. Fdefault,a.fdate,rn "+
" A.fdb,a.fallow,a.fdisallow,a.fsb,a.fbig,a.fsmall, "+
" a.finputmethod,a.fchk,a. Frelation,a.fdesc,a.fsecond,rn "+
" A.fqc,a.fexception,a.fasupp,a.fyqh,a.fpos,a.fstar, "+
" A.FSave,A. Faddress,a.flblcolor,a.fischecklist "+
" from P_field_rule A rn "+
 

"INNER JOIN p_field_initial B on a.fno=b.fno RN" +

"Where a.formtype=" + Common.gIntFormType.ToString () +

"and b.fsection= ' 1 ' and (b.fregion=" + Common.gIntRegion.ToString () +

"or b.fregion=-1) Order by A.forder";

}

Dtget = DB. Getdatatablebysql (strSQL);

if (dtGet.Rows.Count <= 0)

{

Common.showmessage (the Field Rules table has no data, please contact the software engineer immediately! ", Messageboxicon.error);

return false;

}

Get class information, prepare for the reflection call below

Type otype = Type.GetType ("CCB_Donet.ClassFolder.FieldRuleInfo");

Generates an array of class objects, consistent with the number of database records

Mmainfieldrule = new Fieldruleinfo[dtget.rows.count];

for (int i = 0; i < DtGet.Rows.Count; i++)

{

This uses reflection dynamics to assign value to the Fieldruleinfo field

Mmainfieldrule[i] = new Fieldruleinfo ();

for (int j = 0; J < DtGet.Columns.Count; J + +)

{

This gets the field name of the class directly and assigns the value of the corresponding field in the database to it

FieldInfo FieldInfo = Otype.getfield ("gstr" + dtget.columns[j). ColumnName,

BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

| BindingFlags.Static);

Fieldinfo.setvalue (Mmainfieldrule[i], dtget.rows[i][j]. ToString ());

}

}

return true;

#if (DEBUG)

}

catch (Exception ex)

{

return false;

Mylog.writeerrlog ("Frmde-m_getrule", ex. message);

}

Finally

{

Dtget = null;

}

#endif

}

#endregion

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.