C#實現(xiàn)利用反射簡化給類字段賦值的方法
更新時間:2015年05月13日 11:08:23 作者:永遠愛好寫程序
這篇文章主要介紹了C#實現(xiàn)利用反射簡化給類字段賦值的方法,涉及C#操作反射的相關技巧,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)利用反射簡化給類字段賦值的方法。分享給大家供大家參考。具體分析如下:
說明:這個例子主要的思路是建立一個類和數(shù)據(jù)庫查詢語句的字段結構是一致的
然后利用反射,直接用數(shù)據(jù)字段名稱進行拼湊,給類對象的字段進行賦值
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ī)則表沒有數(shù)據(jù),請馬上聯(lián)系軟件工程師!", MessageBoxIcon.Error);
return false;
}
//獲得類信息,為下面的反射調用做準備
Type oType = Type.GetType("CCB_Donet.ClassFolder.FieldRuleInfo");
//生成類對象數(shù)組,和數(shù)據(jù)庫記錄個數(shù)是一致的
mMainFieldRule = new FieldRuleInfo[dtGet.Rows.Count];
for (int i = 0; i < dtGet.Rows.Count; i++)
{
//這里使用反射動態(tài)為FieldRuleInfo字段賦值數(shù)據(jù)
mMainFieldRule[i] = new FieldRuleInfo();
for (int j = 0; j < dtGet.Columns.Count; j++)
{
//這里直接獲取類的字段名稱,然后把數(shù)據(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#程序設計有所幫助。
相關文章
淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別
C#靜態(tài)方法與非靜態(tài)方法的區(qū)別不僅僅是概念上的,那么他們有什么具體的區(qū)別呢?讓我們通過本文向大家介紹下C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別,一起看看吧2017-09-09
C#中Parallel類For、ForEach和Invoke使用介紹
這篇文章介紹了C#中Parallel類For、ForEach和Invoke的使用方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

