Enum IConvertible can convert to same enum or System.Enum. Fixes #15289
authorMarek Safar <marek.safar@gmail.com>
Wed, 9 Oct 2013 12:04:47 +0000 (14:04 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 9 Oct 2013 12:04:47 +0000 (14:04 +0200)
mcs/class/corlib/System/Convert.cs
mcs/class/corlib/System/Enum.cs
mcs/class/corlib/Test/System/EnumTest.cs

index 7d70ee1f75e742f4ced56e3dc91e00729d048862..a17181f38ba2763c00625714120c4795ebb56487 100644 (file)
@@ -2529,6 +2529,7 @@ namespace System {
                        typeof (DateTime), // 16 TypeCode.DateTime
                        null,               // 17 null.
                        typeof (String),   // 18 TypeCode.String
+                       typeof (Enum)
                };
 
                // Function to convert an object to another type and return
@@ -2620,6 +2621,9 @@ namespace System {
                                if (conversionType == conversionTable[18]) // 18 TypeCode.String
                                        return convertValue.ToString (provider);
 
+                               if (conversionType == conversionTable[19] && value is Enum) // System.Enum
+                                       return value;
+
                                if (try_target_to_type)
                                        return convertValue.ToType (conversionType, provider);
                        } 
index c16e604d977e84bbfc257b129bb7956267fc3bdd..ecf2ed189815cec3f6271bac2324ee8ac3d50279 100644 (file)
@@ -278,9 +278,8 @@ namespace System
                {
                        if (targetType == null)
                                throw new ArgumentNullException ("targetType");
-                       if (targetType == typeof (string))
-                               return ToString (provider);
-                       return Convert.ToType (Value, targetType, provider, false);
+
+                       return Convert.ToType (this, targetType, provider, false);
                }
 
                ushort IConvertible.ToUInt16 (IFormatProvider provider)
index f6c0dca2a5932b0aef51d7387f74fc450585ef75..973b068e823ee28ebde1fcd9d94ce270b8aa8a4a 100644 (file)
@@ -849,9 +849,12 @@ namespace MonoTests.System
                }
 
                [Test]
-               public void ConvertToStringType ()
+               public void IConvertible_Valid ()
                {
-                       Assert.AreEqual ("This", ((IConvertible) TestingEnum.This).ToType (typeof (string), null));
+                       IConvertible ic = TestingEnum.This;
+                       Assert.AreEqual ("This", ic.ToType (typeof (string), null), "#1");
+                       Assert.AreEqual (TestingEnum.This, ic.ToType (typeof (TestingEnum), null), "#2");
+                       Assert.AreEqual (TestingEnum.This, ic.ToType (typeof (Enum), null), "#3");
                }
 
                [Test]