Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to test my conversions to avoid using exceptions?

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.

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

So is there any way to look before I leap with these ChangeType conversions?


SOLUTION

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!

like image 494
Alain Avatar asked Oct 25 '11 14:10

Alain


1 Answers

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

like image 163
Stecya Avatar answered Sep 20 '22 12:09

Stecya