Created
November 21, 2011 21:31
-
-
Save gimmi/1384010 to your computer and use it in GitHub Desktop.
IDataRecord to POCO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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