/// <summary> /// Tries to map values from the row to the object using reflection. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rowObj">The new object.</param> /// <param name="row">The row.</param> /// <param name="validator">The validator.</param> /// <param name="errors">The errors.</param> /// <exception cref="System.ArgumentNullException"> /// rowObj /// or /// row /// or /// validator /// or /// errors /// </exception> public static void TryMapValues <T>(T rowObj, dynamic row, IObjectValidator validator, ref List <string> errors) { if (rowObj == null) { throw new ArgumentNullException("rowObj"); } if (row == null) { throw new ArgumentNullException("row"); } if (validator == null) { throw new ArgumentNullException("validator"); } if (errors == null) { throw new ArgumentNullException("errors"); } CustomExpandoObject expando = row as CustomExpandoObject; if (expando == null) { expando = new CustomExpandoObject(row, true); } var getRowValueMethod = validator.GetType().GetMethods().First(x => x.Name == "GetRowValue" && x.GetParameters().Count() >= 5 && x.GetParameters().First().ParameterType == typeof(string)); PropertyInfo[] properties = typeof(T).GetProperties(); foreach (var prop in properties) { if (expando.ContainsKey(prop.Name)) //there may be properties that were not imported { string val = Convert.ToString(expando[prop.Name]); Type nonNullableType = FileIOHelpers.GetNonNullableType(prop.PropertyType); if (nonNullableType.IsValueType) { bool isNullable = Nullable.GetUnderlyingType(prop.PropertyType) != null; //convert the Helpers.GetValue method into a generic method of the same type as the property var genericGetvalueMethod = getRowValueMethod.MakeGenericMethod(new Type[] { nonNullableType }); var value = genericGetvalueMethod.Invoke(validator, new object[] { val, errors, null, isNullable, prop.Name }); prop.SetValue(rowObj, value, null); //this should use the setrowvalue that outputs conversion errors } else { prop.SetValue(rowObj, Convert.ChangeType(val, prop.PropertyType), null); } } } }