views:

37

answers:

1

The Convert class has been in existence since .NET 1.0. The IConvertible interface has also existed since this time.

The Convert.ChangeType method only works on objects of types that implement IConvertible (in fact, unless I'm mistaken, all of the conversion methods provided by the Convert class are this way). So why is the parameter type object?

In other words, instead of this:

public object ChangeType(object value, Type conversionType);

Why isn't the signature this?

public object ChangeType(IConvertible value, Type conversionType);

Just seems strange to me.

+2  A: 

Looking in reflector you can see the top of ChangeType(object, Type, IFormatProvider), which is what's called under the covers:

public static object ChangeType(object value, Type conversionType, IFormatProvider provider)
{
  //a few null checks...
  IConvertible convertible = value as IConvertible;
  if (convertible == null)
  {
    if (value.GetType() != conversionType)
    {
        throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
    }
    return value;
  }

So it looks like an object of a type that doesn't implement IConvertible but already is the destination type will just return the original object.

Granted it looks like this is the only exception to the value needing to implement IConvertible, but it is an exception, and looks like the reason the parameter is object instead.


Here's a quick LinqPad test for this case:

void Main()
{
  var t = new Test();
  var u = Convert.ChangeType(t, typeof(Test));
  (u is IConvertible).Dump();   //false, for demonstration only
  u.Dump();                     //dump of a value Test object
}

public class Test {
  public string Bob;
}
Nick Craver
I guess this does seem to be the only possible explanation... doesn't it seem odd to you, though, that the designers would've considered the behavior "can take an object and convert it to the same type" even worth supporting? I mean, what would this ever be used for?
Dan Tao
@Dan - I think ObjectDatasource uses it internally IIRC, I guess they preferred the check inside instead of one outside? I agree it doesn't seem very useful.
Nick Craver