熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#利用反射簡化給類字段賦值

2013-11-13 10:35:05  來源: .NET編程 

  這個例子主要的思路是建立一個類和數據庫查詢語句的字段結構是一致的

  然後利用反射直接用數據字段名稱進行拼湊給類對象的字段進行賦值

  類的定義

  namespace CCB_DonetClassFolder
{
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 (CommongIntTypeOrder == )
{
strSQL = "select AFNoAFNameAFLockAFCaptionAFType" +
"AFMustAFMinAFMaxAFDefaultAFDatern" +
"AFDBAFAllowAFDisallowAFSBAFBigAFSmallAFInputMethod" +
"AFCHKAFRelationAFDescAFSecondrn" +
"AFQCAFExceptionAFASuppAFYQHAFPosAFStarAFSave"+
"AFAddressAFLblColorAFIsCheckList from P_Field_Rule A rn" +
"INNER JOIN P_Field_Initial B ON AFNo=BFNo rn" +
"where AFormType= AND BFSection= AND " +
"(BFRegion= OR BFRegion=) ORDER BY AFOrder";
}
else
{
strSQL = "select AFNoAFNameAFLockAFCaptionAFType"+
"AFMustAFMinAFMaxAFDefaultAFDatern" +
"AFDBAFAllowAFDisallowAFSBAFBigAFSmall"+
"AFInputMethodAFCHKAFRelationAFDescAFSecondrn" +
"AFQCAFExceptionAFASuppAFYQHAFPosAFStar"+
"AFSaveAFAddressAFLblColorAFIsCheckList "+
"from P_Field_Rule A rn" +
 

  "INNER JOIN P_Field_Initial B ON AFNo=BFNo rn" +

  "where AFormType=" + CommongIntFormTypeToString() +

  " AND BFSection= AND (BFRegion=" + CommongIntRegionToString() +

  " OR BFRegion=) ORDER BY AFOrder";

  }

  dtGet = DBGetDataTableBySQL(strSQL);

  if (dtGetRowsCount <= )

  {

  CommonShowMessage("字段規則表沒有數據請馬上聯系軟件工程師!" MessageBoxIconError);

  return false;

  }

  //獲得類信息為下面的反射調用做准備

  Type oType = TypeGetType("CCB_DonetClassFolderFieldRuleInfo");

  //生成類對象數組和數據庫記錄個數是一致的

  mMainFieldRule = new FieldRuleInfo[dtGetRowsCount];

  for (int i = ; i < dtGetRowsCount; i++)

  {

  //這裡使用反射動態為FieldRuleInfo字段賦值數據

  mMainFieldRule[i] = new FieldRuleInfo();

  for (int j = ; j < dtGetColumnsCount; j++)

  {

  //這裡直接獲取類的字段名稱然後把數據庫裡對應字段的值賦值給它

  FieldInfo fieldInfo = oTypeGetField("gStr" + dtGetColumns[j]ColumnName

  BindingFlagsPublic | BindingFlagsNonPublic | BindingFlagsInstance

  | BindingFlagsStatic);

  fieldInfoSetValue(mMainFieldRule[i] dtGetRows[i][j]ToString());

  }

  }

  return true;

  #if(DEBUG)

  }

  catch (Exception ex)

  {

  return false;

  MyLogWriteErrLog("frmDEm_GetRule" exMessage);

  }

  finally

  {

  dtGet = null;

  }

  #endif

  }

  #endregion


From:http://tw.wingwit.com/Article/program/net/201311/14047.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.