C# 遍历对象属性取值赋值

            TJlb_Mod tJlb = new TJlb_Mod();

            System.Reflection.PropertyInfo[] properties = tJlb.GetType().GetProperties();
            //循环属性取值
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                string name = property.Name;
                string value = property.GetValue(tJlb).ToString();
            }

            //单独给某个属性(FNO)赋值
            tJlb.GetType().GetProperty("FNo").SetValue(tJlb, 11);
            //按照DataTable查询的表字段赋值
            DataTable dt = new DataTable();
            string sql = @"EXEC Proc_GetTJlb_Mod 666";
            dt = new SqlHelper().GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                var dr = dt.Rows[0];
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dr[dc.ColumnName] != DBNull.Value)
                    {
                        var val = dr[dc.ColumnName];
                        PropertyInfo property = typeof(TJlb_Mod).GetProperty(dc.ColumnName);
                        if (property != null)
                        {
                            property.SetValue(tJlb, val, null);
                        }
                    }
                }
            }
原文地址:https://www.cnblogs.com/sky-gfan/p/6541932.html