悬赏园豆:5
[已关闭问题]
<P>类Company有以下属性<BR>companyName,City...<BR>常规方法赋值: <BR>companyName= DataView1["companyName"].ToString();<BR>City=DataView1["City"].ToString();</P>
<P>我想定义属性以后使用反射赋值:<BR> DbDataReader dr = db.ExecuteReader("company");<BR> Company model = new Company();<BR> FillModel(model, dr);<BR>可得到的值为空?<BR>属性名称定义的和数据库定义的相同(包括大小写)。相关函数:<BR> public void FillModel(object targetObj, DbDataReader reader)<BR> {<BR> if (!reader.Read()) { return; }<BR> for (int i = 0; i < reader.FieldCount; i++)<BR> {<BR> System.Reflection.PropertyInfo propertyInfo = targetObj.GetType().GetProperty(reader.GetName(i));<BR> if (propertyInfo != null)<BR> {<BR> if (reader.GetValue(i) != DBNull.Value)<BR> {<BR> propertyInfo.SetValue(targetObj, reader.GetValue(i), null);<BR> System.Web.HttpContext.Current.Response.Write(targetObj.ToString() + ":" + reader.GetValue(i)+"<br>");//注意调用的时候重复输出companyName一个字段,值却是逐个字段的值<BR> }<BR> }<BR> }<BR> }</P>