WEB开发网
开发学院WEB开发ASP.NET C#利用反射简化给类字段赋值 阅读

C#利用反射简化给类字段赋值

 2012-12-05 14:33:38 来源:WEB开发网   
核心提示: 这个例子主要的思路是建立一个类和数据库查询语句的字段结构是一致的然后利用反射,直接用数据字段名称进行拼凑,C#利用反射简化给类字段赋值,给类对象的字段进行赋值1.类的定义namespace CCB_Donet.ClassFolder{public class FieldRuleInfo{public string g

 这个例子主要的思路是建立一个类和数据库查询语句的字段结构是一致的

然后利用反射,直接用数据字段名称进行拼凑,给类对象的字段进行赋值

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;
}
//获得类信息,为下面的反射调用做准备

1 2  下一页

Tags:利用 反射 简化

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接