C# 遍历对象的属性 获取或设置值
--设置值 p.SetValue(parm, row["ParmValue"].ToString(), null);
public Parm GetParm(DataTable dt) { Parm parm = new Parm(); if (dt != null && dt.Rows.Count > 0) { foreach (System.Reflection.PropertyInfo p in parm.GetType().GetProperties()) { foreach (DataRow row in dt.Rows) { if (row["ParmName"].ToString().ToUpper() == p.Name.ToUpper()) { p.SetValue(parm, row["ParmValue"].ToString(), null); break; } } } return parm; } return null; }
--获取值 p.GetValue(parm, null),p.Name);
StringBuilder strSql = new StringBuilder(); string update = " UPDATE AreaParm SET ParmValue={0} WHERE AreaID=" + AreaID + " AND ParmType=" + ParmType + " AND ParmName='{1}'; "; foreach (System.Reflection.PropertyInfo p in parm.GetType().GetProperties()) { string.Format("Name:{0} Value:{1}", p.Name, p.GetValue(parm, null)); strSql.AppendFormat(update, p.GetValue(parm, null),p.Name); } return Dal.DalCommon.ExecuteSqlCmd(new SqlCommand(strSql.ToString()));