Cc using reflection to simplify the assignment of class fields

From , 5 Years ago, written in C#, viewed 80 times.
URL https://pastebin.vip/view/63c4b1ba
  1. namespace CCB_Donet.ClassFolder
  2. {
  3.     public class FieldRuleInfo
  4.     {
  5.         public string gStrFNo;
  6.         public string gStrFName;
  7.         public string gStrFLock;
  8.         public string gStrFCaption;
  9.         public string gStrFType;
  10.         public string gStrFMust;
  11.         public string gStrFMin;
  12.         public string gStrFMax;
  13.         public string gStrFDefault;
  14.         public string gStrFDate;
  15.         public string gStrFDB;
  16.         public string gStrFAllow;
  17.         public string gStrFDisallow;
  18.         public string gStrFSB;
  19.         public string gStrFBig;
  20.         public string gStrFSmall;
  21.         public string gStrFInputMethod;
  22.         public string gStrFCHK;
  23.         public string gStrFRelation;
  24.         public string gStrFDesc;
  25.         public string gStrFSecond;
  26.         public string gStrFQC;
  27.         public string gStrFException;
  28.         public string gStrFASupp;
  29.         public string gStrFYQH;
  30.         public string gStrFPos;
  31.         public string gStrFStar;
  32.         public string gStrFSave;
  33.         public string gStrFAddress;
  34.         public string gStrFLblColor;
  35.         public string gStrFIsCheckList;
  36.     }
  37. }
  38.  
  39.  
  40.  
  41.         #region 加载字段规则
  42.         private bool m_GetRule()
  43.         {
  44.             string strSQL = "";
  45.             DataTable dtGet = null;
  46.  
  47. #if(DEBUG)
  48.             try
  49.             {
  50. #endif
  51.  
  52.                 if (Common.gIntTypeOrder == 95)
  53.                 {
  54.                     strSQL = "select A.FNo,A.FName,A.FLock,A.FCaption,A.FType," +
  55.                          "A.FMust,A.FMin,A.FMax,A.FDefault,A.FDate,\r\n" +
  56.                         "A.FDB,A.FAllow,A.FDisallow,A.FSB,A.FBig,A.FSmall,A.FInputMethod," +
  57.                         "A.FCHK,A.FRelation,A.FDesc,A.FSecond,\r\n" +
  58.                         "A.FQC,A.FException,A.FASupp,A.FYQH,A.FPos,A.FStar,A.FSave,"+
  59.                         "A.FAddress,A.FLblColor,A.FIsCheckList from P_Field_Rule95 A \r\n" +
  60.                         "INNER JOIN P_Field_Initial B ON A.FNo=B.FNo \r\n" +
  61.                         "where A.FormType=1 AND B.FSection='1' AND " +
  62.                          "(B.FRegion95=1 OR B.FRegion95=-1) ORDER BY A.FOrder";
  63.                 }
  64.                 else
  65.                 {
  66.                     strSQL = "select A.FNo,A.FName,A.FLock,A.FCaption,A.FType,"+
  67.                             "A.FMust,A.FMin,A.FMax,A.FDefault,A.FDate,\r\n" +
  68.                             "A.FDB,A.FAllow,A.FDisallow,A.FSB,A.FBig,A.FSmall,"+
  69.                             "A.FInputMethod,A.FCHK,A.FRelation,A.FDesc,A.FSecond,\r\n" +
  70.                             "A.FQC,A.FException,A.FASupp,A.FYQH,A.FPos,A.FStar,"+
  71.                             "A.FSave,A.FAddress,A.FLblColor,A.FIsCheckList "+
  72.                              "from P_Field_Rule A \r\n" +
  73.                             "INNER JOIN P_Field_Initial B ON A.FNo=B.FNo \r\n" +
  74.                             "where A.FormType=" + Common.gIntFormType.ToString() +
  75.                             " AND B.FSection='1' AND (B.FRegion=" + Common.gIntRegion.ToString() +
  76.                             " OR B.FRegion=-1) ORDER BY A.FOrder";
  77.                 }
  78.  
  79.                 dtGet = DB.GetDataTableBySQL(strSQL);
  80.                 if (dtGet.Rows.Count <= 0)
  81.                 {
  82.                     Common.ShowMessage("字段规则表没有数据,请马上联系软件工程师!", MessageBoxIcon.Error);
  83.                     return false;
  84.                 }
  85.                 //获得类信息,为下面的反射调用做准备
  86.                 Type oType = Type.GetType("CCB_Donet.ClassFolder.FieldRuleInfo");
  87.  
  88.                 //生成类对象数组,和数据库记录个数是一致的
  89.                 mMainFieldRule = new FieldRuleInfo[dtGet.Rows.Count];          
  90.                 for (int i = 0; i < dtGet.Rows.Count; i++)
  91.                 {
  92.                     //这里使用反射动态为FieldRuleInfo字段赋值数据
  93.                     mMainFieldRule[i] = new FieldRuleInfo();
  94.                     for (int j = 0; j < dtGet.Columns.Count; j++)
  95.                     {
  96.  
  97.                         //这里直接获取类的字段名称,然后把数据库里对应字段的值赋值给它
  98.                         FieldInfo fieldInfo = oType.GetField("gStr" + dtGet.Columns[j].ColumnName,
  99.                             BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
  100.                                     | BindingFlags.Static);
  101.                         fieldInfo.SetValue(mMainFieldRule[i], dtGet.Rows[i][j].ToString());
  102.                     }
  103.                 }
  104.  
  105.                 return true;
  106. #if(DEBUG)
  107.             }
  108.             catch (Exception ex)
  109.             {
  110.                 return false;
  111.                 MyLog.WriteErrLog("frmDE-m_GetRule", ex.Message);
  112.             }
  113.             finally
  114.             {
  115.                 dtGet = null;
  116.             }
  117. #endif
  118.         }
  119.         #endregion
  120.  
  121.  
  122. //csharp/5802

Reply to "Cc using reflection to simplify the assignment of class fields"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website