Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Last active July 19, 2019 09:24
Show Gist options
  • Save wi7a1ian/4c00cc1940b5d8be7baed1ebb390e472 to your computer and use it in GitHub Desktop.
Save wi7a1ian/4c00cc1940b5d8be7baed1ebb390e472 to your computer and use it in GitHub Desktop.
Map data row from db reader into generic type #csharp
public void MapRowTo<T>(SQLiteDataReader reader, ref T obj)
{
string propertyName;
for (int columnIdx = 0; columnIdx < reader.FieldCount; columnIdx++)
{
propertyName = reader.GetName(columnIdx);
var prop = typeof(T).GetRuntimeProperty(propertyName);
if (prop == null)
{
continue;
}
if (!reader.IsDBNull(columnIdx))
{
prop.SetValue(obj, reader[propertyName]?.ToString());
}
else
{
prop.SetValue(obj, string.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment