欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现利用反射简化给类字段赋值的方法

程序员文章站 2022-04-11 08:20:36
本文实例讲述了c#实现利用反射简化给类字段赋值的方法。分享给大家供大家参考。具体分析如下: 说明:这个例子主要的思路是建立一个类和数据库查询语句的字段结构是一致的 然后...

本文实例讲述了c#实现利用反射简化给类字段赋值的方法。分享给大家供大家参考。具体分析如下:

说明:这个例子主要的思路是建立一个类和数据库查询语句的字段结构是一致的
然后利用反射,直接用数据字段名称进行拼凑,给类对象的字段进行赋值
 
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 加载字段规则
 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("字段规则表没有数据,请马上联系软件工程师!", messageboxicon.error);
   return false;
  }
  //获得类信息,为下面的反射调用做准备
  type otype = type.gettype("ccb_donet.classfolder.fieldruleinfo");
  //生成类对象数组,和数据库记录个数是一致的
  mmainfieldrule = new fieldruleinfo[dtget.rows.count];  
  for (int i = 0; i < dtget.rows.count; i++)
  {
   //这里使用反射动态为fieldruleinfo字段赋值数据
   mmainfieldrule[i] = new fieldruleinfo();
   for (int j = 0; j < dtget.columns.count; 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#程序设计有所帮助。