C # simplify assignment of class fields Using Reflection

Source: Internet
Author: User
Tags flock

The main idea of this example is to establish a field structure consistent with that of the database query statement.

Then, the reflection is used to directly piece together the data field names and assign values to the fields of the Class Object.

1. Class Definition

Namespace CCB_Donet.ClassFolder
{
Public class FieldRuleInfo
{
Public string gStrFNo;
Public string 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 loading field rules
Private bool m_GetRule ()
{
String strSQL = "";
DataTable dtGet = null;

# If (DEBUG)
Try
{
# Endif

If (Common. gIntTypeOrder = 95)
{
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 rule table has no data. Please contact the software engineer immediately! ", MessageBoxIcon. Error );

Return false;

}

// Obtain the class information to prepare for the reflection call below

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

// Generate an array of class objects, which is consistent with the number of database records

MMainFieldRule = new FieldRuleInfo [dtGet. Rows. Count];

For (int I = 0; I <dtGet. Rows. Count; I ++)

{

// Use reflection to dynamically assign values to the FieldRuleInfo field.

MMainFieldRule [I] = new FieldRuleInfo ();

For (int j = 0; j <dtGet. Columns. Count; j ++)

{

// Obtain the field name of the class and assign 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.