private T BuildObject(IDataRecord record) { var obj = CreateNewT(); for (int i = 0; i < _fillInfo.Length; i++) { QuickFillInfo info = _fillInfo[i]; if (info == null) { continue; } object oValue = record.GetValue(i); if (Convert.IsDBNull(oValue)) { continue; } try { if (!info.IsAssignable) { if ((info.PropertyType == typeof(Guid)) && (oValue is string)) { info.PropertyInfo.SetValue(obj, new Guid(oValue.ToString()), null); } else if (info.FieldType.Name.EndsWith("SqlHierarchyId")) // if the column is a HierarchyID type, then just treat it as a string (SQL server can implicitly convert between the two) { info.PropertyInfo.SetValue(obj, oValue.ToString(), null); } else { info.PropertyInfo.SetValue(obj, Convert.ChangeType(oValue, info.PropertyType), null); } } else if ((oValue is DateTime) && (info.MapField.DateTimeKind != DateTimeKind.Unspecified)) { // special date/time handling for UTC and Local times var dtValue = new DateTime(((DateTime)oValue).Ticks, info.MapField.DateTimeKind); info.PropertyInfo.SetValue(obj, dtValue, null); } else { info.PropertyInfo.SetValue(obj, oValue, null); } } catch (Exception ex) { throw new PropertyReadException(info.PropertyInfo, oValue, ex); } } return(obj); }
private void BuildQuickFillArray(DbDataReader reader) { // init quick fill array var outArray = new QuickFillInfo[reader.VisibleFieldCount]; // put field name/ordinal pairs in dictionary for exception free lookup var keyComparer = StringComparer.CurrentCultureIgnoreCase; var readerFields = new Dictionary <string, int>(keyComparer); for (int i = 0; i < reader.VisibleFieldCount; i++) { readerFields.Add(reader.GetName(i), i); } foreach (IDataMapField field in _dataMap.ReadableFields) { int ordinal; if (!readerFields.TryGetValue(field.FieldName, out ordinal)) { continue; } Type propType = field.Property.PropertyType; if (propType.IsEnum) { propType = Enum.GetUnderlyingType(propType); } var qfi = new QuickFillInfo { MapField = field, PropertyInfo = field.Property, PropertyType = propType, FieldType = reader.GetFieldType(ordinal), }; // determine quickly if is assignable qfi.IsAssignable = (propType.IsAssignableFrom(qfi.FieldType)); outArray[ordinal] = qfi; } _fillInfo = outArray; }
private T BuildObject(IDataRecord record) { var obj = new T(); for (int i = 0; i < _fillInfo.Length; i++) { QuickFillInfo info = _fillInfo[i]; if (info == null) { continue; } object oValue = record.GetValue(i); if (Convert.IsDBNull(oValue)) { continue; } try { if (!info.IsAssignable) { if ((info.PropertyType == typeof(Guid)) && (oValue is string)) { info.PropertyInfo.SetValue(obj, new Guid(oValue.ToString()), null); } else { info.PropertyInfo.SetValue(obj, Convert.ChangeType(oValue, info.PropertyType), null); } } else { info.PropertyInfo.SetValue(obj, oValue, null); } } catch (Exception ex) { throw new PropertyReadException(info.PropertyInfo, oValue, ex); } } return(obj); }