C#完成應用反射簡化給類字段賦值的辦法。本站提示廣大學習愛好者:(C#完成應用反射簡化給類字段賦值的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成應用反射簡化給類字段賦值的辦法正文
本文實例講述了C#完成應用反射簡化給類字段賦值的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
解釋:這個例子重要的思緒是樹立一個類和數據庫查詢語句的字段構造是分歧的
然後應用反射,直接用數據字段稱號停止拼集,給類對象的字段停止賦值
1.類的界說
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 加載字段規矩
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,\r\n" +
"A.FDB,A.FAllow,A.FDisallow,A.FSB,A.FBig,A.FSmall,A.FInputMethod," +
"A.FCHK,A.FRelation,A.FDesc,A.FSecond,\r\n" +
"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 \r\n" +
"INNER JOIN P_Field_Initial B ON A.FNo=B.FNo \r\n" +
"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,\r\n" +
"A.FDB,A.FAllow,A.FDisallow,A.FSB,A.FBig,A.FSmall,"+
"A.FInputMethod,A.FCHK,A.FRelation,A.FDesc,A.FSecond,\r\n" +
"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 \r\n" +
"INNER JOIN P_Field_Initial B ON A.FNo=B.FNo \r\n" +
"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("字段規矩表沒稀有據,請立時接洽軟件工程師!", MessageBoxIcon.Error);
return false;
}
//取得類信息,為上面的反射挪用做預備
Type oType = Type.GetType("CCB_Donet.ClassFolder.FieldRuleInfo");
//生成類對象數組,和數據庫記載個數是分歧的
mMainFieldRule = new FieldRuleInfo[dtGet.Rows.Count];
for (int i = 0; i < dtGet.Rows.Count; i++)
{
//這裡應用反射靜態為FieldRuleInfo字段賦值數據
mMainFieldRule[i] = new FieldRuleInfo();
for (int j = 0; j < dtGet.Columns.Count; j++)
{
//這裡直接獲得類的字段稱號,然後把數據庫裡對應字段的值賦值給它
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
願望本文所述對年夜家的C#法式設計有所贊助。