Object.ConvertTo<T> bug?

Dec 15, 2010 at 6:52 AM

Hi,

The following code throws an exception:

  . . .

object _o = "qwerty";
object _o2 = _o.ConvertTo<int>();

So it should be?

 

Developer
Dec 15, 2010 at 8:17 AM

yes it looks like there is a bug in the TypeDescriptor...

			var converter = TypeDescriptor.GetConverter(targetType);
			if (converter != null)
			{
				if (converter.CanConvertFrom(valueType))
					return (T)converter.ConvertFrom(value);
			}

I've posted the question here.

- Michael T.

Developer
Dec 16, 2010 at 6:33 AM
Edited Dec 16, 2010 at 6:33 AM

I've found a solution for this...

I'll commit the solution in my next update.

Thanks for pointing this out @alegn


Close this if you are satisfy

Dec 16, 2010 at 9:37 AM

I saw your solution here This solution is almost equivalent to using the method

 public static T ConvertTo<T>(this object value, T defaultValue, bool ignoreException)

with

ignoreException=true

Use "try/catch" leads to a heavy (in terms of performance) code. Maybe leave AS IS, adding the necessary description? And for the doubtful cases to use the method

 public static T ConvertTo<T>(this object value, T defaultValue, bool ignoreException)

Sorry for my bad English...