足球游戏_中国足彩网¥体育资讯$

C#利用反射简化给类字段赋值
来源:易贤网 阅读:936 次 日期:2014-08-29 14:55:38
温馨提示:易贤网小编为您整理了“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,rn" +

"A.FDB,A.FAllow,A.FDisallow,A.FSB,A.FBig,A.FSmall,A.FInputMethod," +

"A.FCHK,A.FRelation,A.FDesc,A.FSecond,rn" +

"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 rn" +

"INNER JOIN P_Field_Initial B ON A.FNo=B.FNo rn" +

"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,rn" +

"A.FDB,A.FAllow,A.FDisallow,A.FSB,A.FBig,A.FSmall,"+

"A.FInputMethod,A.FCHK,A.FRelation,A.FDesc,A.FSecond,rn" +

"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 rn" +

 

"INNER JOIN P_Field_Initial B ON A.FNo=B.FNo rn" +

"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

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看网络编程
易贤网手机网站地址:C#利用反射简化给类字段赋值
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标