Good afternoon, what I'm trying to do is set the attributes of a list of objects from a table with two for for the table and a foreach for the attributes of the object. I'm stuck on how to assign the value to the attribute.
/*recorre las filas*/
for (var j = 0; j < dt.Rows.Count; j++)
{
var campo = new Campo();
/*se para en cada fila y recorre las columnas*/
for (var i = 0; i < dt.Columns.Count; i++)
{
var nombreCampo = dt.Columns[i].Caption;
/*recorro las propiedades del objeto campo
* y cuando nombreCampo==propertyInfo.Name
* le asigno el valor de la celda segun cordenadas*/
foreach (PropertyInfo propertyInfo in campo.GetType().GetProperties())
{
if (propertyInfo.Name == nombreCampo)
{
propertyInfo.SetValue( Convert.ToString(dt.Rows[j][i]));
}
}
}
}