有什么办法来测试我转换,避免使用异常? [英] Is there any way to test my conversions to avoid using exceptions?

查看:130
本文介绍了有什么办法来测试我转换,避免使用异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从数据行构建任意对象使用反射,当橡胶终于满足了路,我需要从DataRow中取一个值,并将其分配给对象的属性。

I'm constructing arbitrary objects from DataRows using Reflection, and when the rubber finally meets the road, I need to take a value from the DataRow and assign it to the property on the object.

由于数据行可以是充满不支持转换类型,许多这些结果在必须处理的异常。例如,DBnulls可能攀升DataRow中,或者一些数字格式,不很好地转换为另一种,等有没有什么方法可以让我避免抛出异常? (我不会接受巨大的switch语句检查的目标对象属性和源数据类型的每一个组合。)

Because DataRows can be full of types that don't support conversion, many of these result in exceptions that have to be handled. For example, DBnulls might creep up in the DataRow, or some numeric format that doesn't nicely convert to another, etc. Is there any way I can avoid throwing exceptions? (I won't accept gigantic switch statements checking for every combination of types in the target object property and source data.)

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,这是我想出来的:

Thanks to Stecya, here's what I've come up with:

     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);

在逻辑上等同,优雅的,并没有更多的例外!

Logically equivalent, elegant, and no more exceptions!

推荐答案

使用<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx">TypeConverter要检查是否可以类型之间转换

Use TypeConverter to check if you can convert between types

bool CanConvert(Type from , Type to)
{
    TypeConverter converter = TypeDescriptor.GetConverter(to);
    return converter != null && converter.CanConvertFrom(from);
}

你也可以使用此类型转换器实际上从一种类型转变成另一种

Also you can use this TypeConverter to actually convert from one type to another

这篇关于有什么办法来测试我转换,避免使用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆