Implement TypeConverter.ConvertTo cultureInfo support. Fixes #6322
authorMarek Safar <marek.safar@gmail.com>
Thu, 9 Aug 2012 09:03:36 +0000 (10:03 +0100)
committerMarek Safar <marek.safar@gmail.com>
Thu, 9 Aug 2012 09:03:36 +0000 (10:03 +0100)
mcs/class/System/System.ComponentModel/TypeConverter.cs
mcs/class/System/Test/System.ComponentModel/TypeConverterTests.cs

index a9a6a039f755418bece3451e9edc30641640ee1d..9db2b00f690f9a2b833fb433c93ed4ba668628ca 100644 (file)
@@ -112,14 +112,18 @@ namespace System.ComponentModel
                public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
                                                 Type destinationType)
                {
-                       // context? culture?
+                       // context?
                        if (destinationType == null)
                                throw new ArgumentNullException ("destinationType");
 
                        if (destinationType == typeof (string)) {
-                               if (value != null)
-                                       return value.ToString();
-                               return String.Empty;
+                               if (value == null)
+                                       return String.Empty;
+
+                               if (culture != null)
+                                       return Convert.ToString (value, culture);
+
+                               return value.ToString();
                        }
 
                        return GetConvertToException (value, destinationType);
index 1321800009473c3e6d34f4733f885ca9ddac8c92..2def1613faf8600efcf0b26c8d5445cccc33aa63 100644 (file)
@@ -10,6 +10,7 @@
 using System;
 using System.ComponentModel;
 using System.ComponentModel.Design.Serialization;
+using System.Globalization;
 
 using NUnit.Framework;
 
@@ -162,8 +163,16 @@ namespace MonoTests.System.ComponentModel
                                Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
                        }
                }
+               
+               [Test]
+               public void ConvertToWithCulture ()
+               {
+                       var culture = CultureInfo.CreateSpecificCulture ("sv-se");
+                       
+                       var converter = TypeDescriptor.GetConverter (typeof (string));
+                       Assert.AreEqual ("0,5", (string) converter.ConvertTo (null, culture, 0.5, typeof (string)));
+               }
 
-#if NET_2_0
                public class FooConverter<T> : TypeConverter
                {
                }
@@ -178,7 +187,6 @@ namespace MonoTests.System.ComponentModel
                {
                        Assert.IsNotNull (GetType ().GetProperty ("FooProperty").GetCustomAttributes (false));
                }
-#endif
 
                [ExpectedException (typeof (NullReferenceException))]
                public void GetConvertToException_DestinationType_Null ()