Last active
July 19, 2019 09:24
-
-
Save wi7a1ian/4c00cc1940b5d8be7baed1ebb390e472 to your computer and use it in GitHub Desktop.
Map data row from db reader into generic type #csharp
This file contains hidden or 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
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