Created
January 19, 2017 17:05
-
-
Save julesx/02d8d3adada1d56f7bb32a86285060b9 to your computer and use it in GitHub Desktop.
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
private TSqlRecord MapToClass<TSqlRecord>(SqlDataReader reader) where TSqlRecord : class, ISqlRecord | |
{ | |
var returnedObject = Activator.CreateInstance<TSqlRecord>(); | |
try | |
{ | |
var modelProperties = returnedObject.GetType().GetProperties(); | |
var schemeTable = reader.GetSchemaTable(); | |
foreach (var t in modelProperties) | |
{ | |
var attributes = (MappingAttribute[])Attribute.GetCustomAttributes(t, typeof (MappingAttribute), true); | |
if (attributes.Length > 0 && attributes[0].ColumnName != null) | |
{ | |
dynamic value = null; | |
if (schemeTable?.Rows.Cast<DataRow>().Any(row => row["ColumnName"].ToString() == attributes[0].ColumnName) != true) | |
continue; | |
if (reader[attributes[0].ColumnName] != DBNull.Value) | |
{ | |
var tType = t.PropertyType; | |
if (t.PropertyType.IsGenericType && t.PropertyType.GetGenericTypeDefinition() == typeof (Nullable<>)) | |
{ | |
tType = Nullable.GetUnderlyingType(t.PropertyType); | |
} | |
value = Convert.ChangeType(reader[attributes[0].ColumnName], tType); | |
} | |
t.SetValue(returnedObject, value, null); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
_nLogger.Error(ex); | |
} | |
return returnedObject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment