【发布时间】:2011-10-25 23:45:09
【问题描述】:
我正在使用反射从 DataRows 构造任意对象,当橡胶最终遇到道路时,我需要从 DataRow 中获取一个值并将其分配给对象上的属性。
因为 DataRows 可能充满了不支持转换的类型,其中许多会导致必须处理的异常。例如,DBnulls 可能会出现在 DataRow 中,或者某些不能很好地转换为另一种格式的数字格式,等等。有什么办法可以避免抛出异常吗? (我不会接受检查目标对象属性和源数据中每种类型组合的巨大 switch 语句。)
public void Merge(DataRow data)
{
PropertyInfo[] props = this.GetType().GetProperties(BindingFlags...);
foreach (PropertyInfo pi in props.Where(p => T_IsPrimitive(p.PropertyType)))
{
object src = null;
if( dataAsDataRow.Table.Columns.Contains(pi.Name) )
src = ((DataRow)data)[pi.Name];
if (src != null)
{
if( src.GetType() != pi.PropertyType ) {
try {
src = Convert.ChangeType(src, pi.PropertyType);
} catch(InvalidCastException e) {
try { src = Convert.ChangeType(src.ToString(), pi.PropertyType); }
catch { throw e; }
}
}
pi.SetValue(this, src, null);
}
}
}
public bool T_IsPrimitive(Type t)
{
return t.IsPrimitive || t == typeof(Decimal) || t == typeof(String) ||
t == typeof(DateTime) || t == typeof(TimeSpan)
}
那么在我进行这些ChangeType 转换之前,有什么办法可以查看?
解决方案
感谢 Stecya,这就是我想出的:
if( src.GetType() != pi.PropertyType ) {
object converted = null;
TypeConverter converter = TypeDescriptor.GetConverter(pi.PropertyType);
if( converter != null ) {
if( converter.CanConvertFrom(vType) )
converted = converter.ConvertFrom(src);
else if( converter.CanConvertFrom(typeof(String)) )
converted = converter.ConvertFrom(src.ToString());
}
src = converted;
}
if( src != null )
pi.SetValue(this, src, null);
逻辑上等价,优雅,没有更多的例外!
【问题讨论】:
标签: c# .net-4.0 exception-handling