[Mono-bugs] [Bug 67780][Wis] New - Mono's "Convert.ChangeType" behaving differently than MS.NET
bugzilla-daemon@bugzilla.ximian.com
bugzilla-daemon@bugzilla.ximian.com
Thu, 7 Oct 2004 15:40:23 -0400 (EDT)
Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.
Changed by marianoa@itcsoluciones.com.
http://bugzilla.ximian.com/show_bug.cgi?id=67780
--- shadow/67780 2004-10-07 15:40:23.000000000 -0400
+++ shadow/67780.tmp.13788 2004-10-07 15:40:23.000000000 -0400
@@ -0,0 +1,97 @@
+Bug#: 67780
+Product: Mono: Class Libraries
+Version: unspecified
+OS: GNU/Linux [Other]
+OS Details:
+Status: NEW
+Resolution:
+Severity:
+Priority: Wishlist
+Component: System
+AssignedTo: mono-bugs@ximian.com
+ReportedBy: marianoa@itcsoluciones.com
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL:
+Cc:
+Summary: Mono's "Convert.ChangeType" behaving differently than MS.NET
+
+I have this small function:
+
+public static object Parse(object from, Type toType, object defaultValue) {
+ try {
+ return Convert.ChangeType(from, toType);
+ } catch (Exception) {
+ return defaultValue;
+ }
+}
+
+I use it in Ms.net to convert between types. For example:
+
+int convertedInt = (int) Parse("123", typeof(int), 0); // returns 123
+int convertedInt = (int) Parse("hello", typeof(int), 0); // returns 0
+int convertedInt = (int) Parse(null, typeof(int), 0); // returns 0
+
+So far so good.
+
+Now with mono 1.0 the last statement generates an exception because Parse
+returns null instead of 0. That means that the conversion from null to int
+succeeds and the result is null (That's wrong right?).
+
+Steps to reproduce the problem:
+1. Compile the following source: ConvertTest.cs
+
+using System;
+using NUnit.Framework;
+
+[TestFixture]
+public class ConvertValueTypeToNull {
+ [Test]
+ public void testConvertValueTypes() {
+ // All are value types and cannot be converted to
+ // nulls. This function should return 0, not null.
+ Assert.IsNotNull(Parse(null, typeof(int), 0), "Wrong!");
+ }
+
+ public static object Parse(object from,
+ Type toType,
+ object defaultValue) {
+ try {
+ return Convert.ChangeType(from, toType);
+ } catch (Exception) {
+ return defaultValue;
+ }
+ }
+
+ public static void Main(string [] args) {
+ if (Parse(null, typeof(int), 0) == null) {
+ Console.WriteLine("Error!");
+ } else {
+ Console.WriteLine("Cool!");
+ }
+ }
+}
+
+2. Compile the source with:
+
+$ mcs *.cs -target:exe -r:nunit.framework -debug+
+
+3. Run the program with:
+
+$ mono ConvertTest.exe
+
+4. Optionally it can be run as a test for nunit with:
+
+$ mono nunit-console.exe ConvertTest.exe
+
+Actual Results:
+ConvertTest.exe prints out "Error!".
+
+Expected Results:
+ConvertTest.exe should print out "Cool".
+
+How often does this happen?
+Always.
+
+Additional Information:
+ConvertTest.exe works as expected in ms.net