Thursday, 18 January 2007


Just like DbCommand objects, using DataReader objects is a pain.
Everytime the same routine: check value, cast, set.
I've automated this with RTTI, too:

public class DbDataReaderReviver
public object ActivateObject(DbDataReader reader, Type typeOfObject)
object myObj = System.Activator.CreateInstance(typeOfObject);
FieldInfo[] objFields = typeOfObject.GetFields(BindingFlags.SetField);
string fieldName;
object fieldValue;
foreach (FieldInfo finfo in objFields)
fieldName = finfo.Name;
fieldValue = reader[fieldName];
if (fieldValue == null) continue;
finfo.SetValue(myObj, fieldValue);
return myObj;

No comments: