So I have thousands of objects, of generic type T, and I want to cast them to an array of objects I got. So I have to get the properties list T, and for each property set the value to the corresponding object on the array
for (int i = 0; reader.Read(); i++)
{
T tmp = (T)Activator.CreateInstance(typeof(T));
foreach (var prop in properties)
{
prop.SetValue(tmp, reader.GetValue(reader.GetOrdinal(prop.Name)), null);
}
}
reader is a DataReader. The problem I have is that prop.SetValue is sadly slow (consumes like 50% of the total excecution time), I ve been told to use Dynamic Methods or Expression Trees, I tried using expression trees but from what I understood I have to generate one tree for each value I want to set, which wouldn t be so good. So Dynamic Methods is the other option. Ideally I d need to create a method SetProp(object, propertyName, value) which I can reuse over and over again.