C#利用反射簡化給類字段賦值
來源:易賢網(wǎng) 閱讀:958 次 日期:2014-08-29 14:55:38
溫馨提示:易賢網(wǎng)小編為您整理了“C#利用反射簡化給類字段賦值”,方便廣大網(wǎng)友查閱!

這個(gè)例子主要的思路是建立一個(gè)類和數(shù)據(jù)庫查詢語句的字段結(jié)構(gòu)是一致的然后利用反射,直接用數(shù)據(jù)字段名稱進(jìn)行拼湊,給類對象的字段進(jìn)行賦值

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 加載字段規(guī)則

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("字段規(guī)則表沒有數(shù)據(jù),請馬上聯(lián)系軟件工程師!", MessageBoxIcon.Error);

return false;

}

//獲得類信息,為下面的反射調(diào)用做準(zhǔn)備

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

//生成類對象數(shù)組,和數(shù)據(jù)庫記錄個(gè)數(shù)是一致的

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

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

{

//這里使用反射動(dòng)態(tài)為FieldRuleInfo字段賦值數(shù)據(jù)

mMainFieldRule[i] = new FieldRuleInfo();

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

{

//這里直接獲取類的字段名稱,然后把數(shù)據(jù)庫里對應(yīng)字段的值賦值給它

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

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:C#利用反射簡化給類字段賦值
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)