2008-07-16 Ivan N. Zlatev <contact@i-nz.net>
authorIvan Zlatev <ivan@ivanz.com>
Wed, 16 Jul 2008 21:09:41 +0000 (21:09 -0000)
committerIvan Zlatev <ivan@ivanz.com>
Wed, 16 Jul 2008 21:09:41 +0000 (21:09 -0000)
* TypeDescriptor.cs:
 - Fix GetConverter (Type) overload to have same logic as the
 object one.
 - Object-ReferenceConverter should be the last entry in the predefined
 converters table, so that when we look up we don't end up with the wrong
 converter for derived types.
* TypeDescriptorTests.cs: Add a test that both GetConverter
overloads return the same converter.

svn path=/trunk/mcs/; revision=108087

mcs/class/System/System.ComponentModel/ChangeLog
mcs/class/System/System.ComponentModel/TypeDescriptor.cs
mcs/class/System/Test/System.ComponentModel/ChangeLog
mcs/class/System/Test/System.ComponentModel/TypeDescriptorTests.cs

index aa1e1636258cc2adb7a47a4cdc5f9bfe5ccf07c6..8a42bc1bac4b66723f476395a216cfb1932602ad 100644 (file)
@@ -1,3 +1,12 @@
+2008-07-16  Ivan N. Zlatev  <contact@i-nz.net>
+
+       * TypeDescriptor.cs: 
+        - Fix GetConverter (Type) overload to have same logic as the 
+        object one.
+        - Object-ReferenceConverter should be the last entry in the predefined 
+        converters table, so that when we look up we don't end up with the wrong 
+        converter for derived types.
+
 2008-07-16  Jonathan Pobst  <monkey@jpobst.com>
 
        Apply patch from Achille Fouilleul (achille.fouilleul at gadz dot org)
index d602fdb3284c6d801958aac7c96cc4a805a3a194..f69498b3cb13279de288f652bc26a5526d76a82f 100644 (file)
@@ -304,6 +304,8 @@ public sealed class TypeDescriptor
                        TypeConverterAttribute tca = (TypeConverterAttribute) atts[typeof(TypeConverterAttribute)];
                        if (tca != null && tca.ConverterTypeName.Length > 0)
                                converterType = GetTypeFromName (component as IComponent, tca.ConverterTypeName);
+                       if (converterType == null)
+                               converterType = FindDefaultConverterType (component.GetType ());
                        
                        if (converterType != null) {
                                ConstructorInfo ci = converterType.GetConstructor (new Type[] { typeof(Type) });
@@ -311,9 +313,8 @@ public sealed class TypeDescriptor
                                        return (TypeConverter) ci.Invoke (new object[] { component.GetType () });
                                else
                                        return (TypeConverter) Activator.CreateInstance (converterType);
-                       }
-                       else
-                               return GetConverter (component.GetType ());
+                       } else
+                               return null;
                }
        }
 
@@ -339,7 +340,6 @@ public sealed class TypeDescriptor
                                defaultConverters.Add (typeof (float), typeof (SingleConverter));
                                defaultConverters.Add (typeof (double), typeof (DoubleConverter));
                                defaultConverters.Add (typeof (decimal), typeof (DecimalConverter));
-                               defaultConverters.Add (typeof (object), typeof (TypeConverter));
                                defaultConverters.Add (typeof (void), typeof (TypeConverter));
                                defaultConverters.Add (typeof (Array), typeof (ArrayConverter));
                                defaultConverters.Add (typeof (CultureInfo), typeof (CultureInfoConverter));
@@ -347,6 +347,8 @@ public sealed class TypeDescriptor
                                defaultConverters.Add (typeof (Guid), typeof (GuidConverter));
                                defaultConverters.Add (typeof (TimeSpan), typeof (TimeSpanConverter));
                                defaultConverters.Add (typeof (ICollection), typeof (CollectionConverter));
+                               defaultConverters.Add (typeof (Enum), typeof (EnumConverter));
+                               defaultConverters.Add (typeof (object), typeof (TypeConverter));
                        }
                        return defaultConverters;
                }
@@ -354,48 +356,26 @@ public sealed class TypeDescriptor
        
        public static TypeConverter GetConverter (Type type)
        {
-               TypeConverterAttribute tca = null;
-               Type t = null;
-               object [] atts = type.GetCustomAttributes (typeof(TypeConverterAttribute), true);
-               
-               if (atts.Length > 0)
-                       tca = (TypeConverterAttribute)atts[0];
-               
-               if (tca != null) {
-                       t = GetTypeFromName (null, tca.ConverterTypeName);
-               }
-               
-               if (t == null) {
-                       if (type.IsEnum) {
-                               // EnumConverter needs to know the enum type
-                               return new EnumConverter(type);
-                       } else if (type.IsArray) {
-                               return new ArrayConverter ();
-                       }
-               }
-               
-               if (t == null)
-                       t = FindConverterType (type);
-
-               if (t != null) {
-                       Exception exc = null;
-                       try {
-                               return (TypeConverter) Activator.CreateInstance (t);
-                       } catch (MissingMethodException e) {
-                               exc = e;
-                       }
-
-                       try {
-                               return (TypeConverter) Activator.CreateInstance (t, new object [] {type});
-                       } catch (MissingMethodException) {
-                               throw exc;
-                       }
+               Type converterType = null;
+               AttributeCollection atts = GetAttributes (type);
+               TypeConverterAttribute tca = (TypeConverterAttribute) atts[typeof(TypeConverterAttribute)];
+               if (tca != null && tca.ConverterTypeName.Length > 0)
+                       converterType = GetTypeFromName (null, tca.ConverterTypeName);
+               if (converterType == null)
+                       converterType = FindDefaultConverterType (type);
+
+               if (converterType != null) {
+                       ConstructorInfo ci = converterType.GetConstructor (new Type[] { typeof(Type) });
+                       if (ci != null)
+                               return (TypeConverter) ci.Invoke (new object[] { type });
+                       else
+                               return (TypeConverter) Activator.CreateInstance (converterType);
                }
-
-               return new ReferenceConverter (type);    // Default?
+               else
+                       return null;
        }
 
-       private static Type FindConverterType (Type type)
+       private static Type FindDefaultConverterType (Type type)
        {
                Type t = null;
                
@@ -413,9 +393,9 @@ public sealed class TypeDescriptor
                
                // Nothing found, try the same with our base type
                if (type.BaseType != null)
-                       return FindConverterType (type.BaseType);
+                       return FindDefaultConverterType (type.BaseType);
                else
-                       return null;
+                       return typeof (ReferenceConverter);
        }
 
        public static EventDescriptor GetDefaultEvent (Type componentType)
index 2afa4018b3dedb4faef62c8c58496957a69da1e3..38145364480531fe7b9d9f43ef4883eb6bd795bb 100644 (file)
@@ -1,3 +1,8 @@
+2008-07-16  Ivan N. Zlatev  <contact@i-nz.net>
+
+       * TypeDescriptorTests.cs: Add a test that both GetConverter 
+       overloads return the same converter.
+
 2008-07-16  Jonathan Pobst  <monkey@jpobst.com>
 
        * BackgroundWorkerTest.cs: Add some asserts for bug #364365.
index 444bdc1ce1e4c8e44492605535e7b916839be15a..3424015c6fafb2d1f045e791b1cd13e3ed3beb7b 100644 (file)
@@ -650,6 +650,8 @@ namespace MonoTests.System.ComponentModel
                        // Test from bug #76686
                        Assert.AreEqual  (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28");
 #endif
+                       Assert.IsTrue (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
+                       Assert.IsTrue (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
                }
                
                [Test]