C#實(shí)現(xiàn)利用反射簡(jiǎn)化給類字段賦值的方法
本文實(shí)例講述了C#實(shí)現(xiàn)利用反射簡(jiǎn)化給類字段賦值的方法。分享給大家供大家參考。具體分析如下:
說(shuō)明:這個(gè)例子主要的思路是建立一個(gè)類和數(shù)據(jù)庫(kù)查詢語(yǔ)句的字段結(jié)構(gòu)是一致的
然后利用反射,直接用數(shù)據(jù)字段名稱進(jìn)行拼湊,給類對(duì)象的字段進(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,\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("字段規(guī)則表沒(méi)有數(shù)據(jù),請(qǐng)馬上聯(lián)系軟件工程師!", MessageBoxIcon.Error); return false; } //獲得類信息,為下面的反射調(diào)用做準(zhǔn)備 Type oType = Type.GetType("CCB_Donet.ClassFolder.FieldRuleInfo"); //生成類對(duì)象數(shù)組,和數(shù)據(jù)庫(kù)記錄個(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ù)庫(kù)里對(duì)應(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
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#中sqlDataRead 的三種方式遍歷讀取各個(gè)字段數(shù)值的方法
- C#實(shí)體對(duì)象序列化成Json并讓字段的首字母小寫(xiě)的兩種解決方法
- C#程序連接數(shù)據(jù)庫(kù)及讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié)
- 用C#將圖片保存至Oracle BLOB字段中的方法
- C#更新SQLServer中TimeStamp字段(時(shí)間戳)的方法
- C# Dynamic關(guān)鍵字之:調(diào)用屬性、方法、字段的實(shí)現(xiàn)方法
- C#三種判斷數(shù)據(jù)庫(kù)中取出的字段值是否為空(NULL) 的方法
- c# 類型的字段和方法設(shè)計(jì)建議
相關(guān)文章
C#實(shí)現(xiàn)前向最大匹、字典樹(shù)(分詞、檢索)的示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)前向最大匹、字典樹(shù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05WinForm通過(guò)操作注冊(cè)表實(shí)現(xiàn)限制軟件使用次數(shù)的方法
這篇文章主要介紹了WinForm通過(guò)操作注冊(cè)表實(shí)現(xiàn)限制軟件使用次數(shù)的方法,結(jié)合實(shí)例形式分析了WinForm操作注冊(cè)表的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB詳解
在本篇文章里小編給大家整理的是關(guān)于C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-08-08Unity實(shí)現(xiàn)滑動(dòng)更換界面效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)滑動(dòng)更換界面效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07c#使用nsoup解析html亂碼解決方法分享 nsoup教程
NSoup是JSoup的Net移植版本。使用方法基本一致。如果項(xiàng)目涉及HTML的處理,強(qiáng)烈推薦NSoup。但是遺憾的是NSoup默認(rèn)的編碼是UTF-8,處理中文有亂碼,下面給出二種解決方法2014-01-01C#與C++之間類型的對(duì)應(yīng)知識(shí)點(diǎn)總結(jié)
這篇文章主要介紹了C#與C++之間類型的對(duì)應(yīng)知識(shí)點(diǎn)總結(jié),對(duì)此有需要的朋友們可以參考下。2019-08-08