Skip to content

Instantly share code, notes, and snippets.

@gimmi
Created November 21, 2011 21:31
Show Gist options
  • Save gimmi/1384010 to your computer and use it in GitHub Desktop.
Save gimmi/1384010 to your computer and use it in GitHub Desktop.
IDataRecord to POCO
private static T DefaultConverter<T>(IDataRecord rec)
{
var obj = Activator.CreateInstance<T>();
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
for(int i = 0; i < rec.FieldCount; i++)
{
PropertyDescriptor property = properties.Find(rec.GetName(i), true);
object value = rec.GetValue(i);
if(property != null && value != DBNull.Value)
{
property.SetValue(obj, value);
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment