不幸的是 table.CreateInstance() 是一个相当幼稚的方法,并且对于 Enums 失败(除其他外)。我最终为表编写了一个扩展方法,该方法使用反射来询问它试图创建的对象实例。这种方法的好处是该方法只会覆盖表列中指定的实例值,但是,您必须为表的每一行调用它并传入一个已经实例化的实例。它可以很容易地修改为就像 CreateInstance 方法一样,但就我的目的而言,这效果更好......
public static class TableExtensions
{
public static void FillInstance(this Table table, TableRow tableRow, object instance) {
var propertyInfos = instance.GetType().GetProperties();
table.Header.Each(header => Assert.IsTrue(propertyInfos.Any(pi => pi.Name == header), "Expected to find the property [{0}] on the object of type [{1}]".FormatWith(header, instance.GetType().Name)));
var headerProperties = table.Header.Select(header => propertyInfos.Single(p => p.Name == header)).ToList();
foreach (var propertyInfo in headerProperties) {
object propertyValue = tableRow[propertyInfo.Name];
var propertyType = propertyInfo.PropertyType;
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
propertyType = propertyType.GetGenericArguments().Single();
}
var parse = propertyType.GetMethod("Parse", new[] { typeof(string) });
if (parse != null) {
// ReSharper disable RedundantExplicitArrayCreation
try {
propertyValue = propertyType.Name.Equals("DateTime") ? GeneralTransformations.TranslateDateTimeFrom(propertyValue.ToString()) : parse.Invoke(null, new object[] { propertyValue });
} catch (Exception ex) {
var message = "{0}\r\nCould not parse value: {2}.{3}(\"{1}\")".FormatWith(ex.Message, propertyValue, parse.ReturnType.FullName, parse.Name);
throw new Exception(message, ex);
}
// ReSharper restore RedundantExplicitArrayCreation
}
propertyInfo.SetValue(instance, propertyValue, null);
}
}
}
您可以按如下方式调用它:
ObjectDTO objectDTO;
foreach (var tableRow in table.Rows)
{
objectDTO = table.FillInstance(tableRow, new ObjectDTO());
//Do individual row processing/testing here...
}