samedi 27 février 2016

Reflexion: How to deal with

This question already has an answer here:

I'have a class that's 57 or so properties. I'd like to loop through all the properties and set the value through reflextion.

public class Employee
{
  public Emmployee(DataRow row)
  {
    foreach (PropertyInfo pInfo in this.GetType().GetProperties())
    {
      Type t = pInfo.PropertyType;
      pInfo.SetValue(this, row.Field<t>(pInfo.Name));
    }
  }

  //over 57 properties...
}

I'm getting the following error: t is a variable but is used like a type.





Aucun commentaire:

Enregistrer un commentaire