[Mono-list] System.Convert.ChangeType possible bug.

Martin Garton martin@wrasse.demon.co.uk
Fri, 17 Sep 2004 17:26:13 +0100 (BST)


Hi,

I think there is a bug in System.Convert.ChangeType()

I seems that when an IConvertable is passed but the target type is 
something that cannot be converted to, instead of failing it simply 
returns the unconverted object.

Consider the following example where the caller is trying to (illegally)
convert a string to an emumeration type. I would expect an exception, but
instead a string is returned.

class MainClass
{
  public static void Main(string[] args)
    {
      try {
        object o = Convert.ChangeType("Apple", typeof(Fruit));
        Console.WriteLine("Should not get here. o has type" + o.GetType());
      } catch (InvalidCastException e) {
        Console.WriteLine("Should get here");
      }
    }
}

public enum Fruit {
  Apple,
  Banana,
  Orange
}

The exception is not thrown by ChangeType() and so the catch block is not 
reached.

Does anyone agree that this is a bug?

Incidentally running this on the the ms runtime I got the behaviour I
expected. If we agree this is a bug I will raise it and investigate
further.

Regards,
Martin Garton.