Employee emp = new Employee();
PropertyInfo pi = emp.GetType().GetProperty("myPropertyName");
PropertyInfo pi = emp.GetType().GetProperty("myPropertyName");
pi.SetValue(emp, "myValue", null);
This is how you read a value:
string value = pi.GetValue(emp, null);
To access all properties of an object:
foreach (PropertyInfo info in emp.GetType().GetProperties()){
....
}
An example using a DataReader:
List <employee>list = new List <employee>();
while (dataReader.Read())
{
Employee emp = new Employee();
foreach (PropertyInfo info in emp.GetType().GetProperties())
{
if (!DBNull.Value.Equals(dr[info.Name]))
info.SetValue(emp, dr[info.Name], null);
}
list.Add(emp);
}
No comments:
Post a Comment