Fix inconsistent behavior in VersionConverter and UriTypeConverter with full framewor...
authorRoman Marusyk <Marusyk@users.noreply.github.com>
Mon, 8 Jul 2019 17:08:37 +0000 (20:08 +0300)
committerSantiago Fernandez Madero <safern@microsoft.com>
Mon, 8 Jul 2019 17:08:37 +0000 (10:08 -0700)
* Fix inconsistent behavior in VersionConverter and UriTypeConverter with full framework

* Add test cases

* Add using

Commit migrated from https://github.com/dotnet/corefx/commit/ed5b558c8637dc9628ff62322784975238baaf88

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/UriTypeConverter.cs
src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/VersionConverter.cs
src/libraries/System.ComponentModel.TypeConverter/tests/UriTypeConverterTests.cs
src/libraries/System.ComponentModel.TypeConverter/tests/VersionConverterTests.cs

index 9793761..d093dbf 100644 (file)
@@ -22,12 +22,7 @@ namespace System
         /// </summary>
         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
         {
-            if (sourceType == null)
-            {
-                throw new ArgumentNullException(nameof(sourceType));
-            }
-
-            return sourceType == typeof(string) || sourceType == typeof(Uri);
+            return sourceType == typeof(string) || sourceType == typeof(Uri) || base.CanConvertFrom(context, sourceType);
         }
 
         /// <summary>
@@ -36,7 +31,7 @@ namespace System
         /// </summary>
         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
         {
-            return destinationType == typeof(string) || destinationType == typeof(Uri) || destinationType == typeof(InstanceDescriptor);
+            return destinationType == typeof(Uri) || destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
         }
 
         /// <summary>
@@ -78,9 +73,9 @@ namespace System
             {
                 if (destinationType == typeof(InstanceDescriptor))
                 {
-                    ConstructorInfo ci = typeof(Uri).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(UriKind) }, null);
-                    Debug.Assert(ci != null, "Couldn't find constructor");
-                    return new InstanceDescriptor(ci, new object[] { uri.OriginalString, uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative });
+                    ConstructorInfo ctor = typeof(Uri).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(UriKind) }, null);
+                    Debug.Assert(ctor != null, "Couldn't find constructor");
+                    return new InstanceDescriptor(ctor, new object[] { uri.OriginalString, uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative });
                 }
 
                 if (destinationType == typeof(string))
index 23852ab..9cd317a 100644 (file)
@@ -2,7 +2,10 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.ComponentModel.Design.Serialization;
+using System.Diagnostics;
 using System.Globalization;
+using System.Reflection;
 
 namespace System.ComponentModel
 {
@@ -18,10 +21,7 @@ namespace System.ComponentModel
         /// </summary>
         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
         {
-            if (sourceType == null)
-                throw new ArgumentNullException(nameof(sourceType));
-
-            return sourceType == typeof(string) || sourceType == typeof(Version);
+            return sourceType == typeof(string) || sourceType == typeof(Version) || base.CanConvertFrom(context, sourceType);
         }
 
         /// <summary>
@@ -30,7 +30,7 @@ namespace System.ComponentModel
         /// </summary>
         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
         {
-            return destinationType == typeof(string) || destinationType == typeof(Version);
+            return destinationType == typeof(Version) || destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
         }
 
         /// <summary>
@@ -72,11 +72,22 @@ namespace System.ComponentModel
 
             if (value is Version version)
             {
+                if (destinationType == typeof(InstanceDescriptor))
+                {
+                    ConstructorInfo ctor = typeof(Version).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }, null);
+                    Debug.Assert(ctor != null, "Couldn't find constructor");
+                    return new InstanceDescriptor(ctor, new object[] { version.Major, version.Minor, version.Build, version.Revision });
+                }
+
                 if (destinationType == typeof(string))
+                {
                     return version.ToString();
+                }
 
                 if (destinationType == typeof(Version))
+                {
                     return new Version(version.Major, version.Minor, version.Build, version.Revision);
+                }
             }
 
             return base.ConvertTo(context, culture, value, destinationType);
index c11753b..3f44d39 100644 (file)
@@ -15,10 +15,11 @@ namespace System.ComponentModel.Tests
         [Fact]
         public static void CanConvertFrom_WithContext()
         {
-            CanConvertFrom_WithContext(new object[2, 2]
+            CanConvertFrom_WithContext(new object[3, 2]
                 {
                     { typeof(string), true },
-                    { typeof(Uri), true }
+                    { typeof(Uri), true },
+                    { typeof(InstanceDescriptor), true }
                 },
                 UriTypeConverterTests.s_converter);
         }
@@ -30,7 +31,7 @@ namespace System.ComponentModel.Tests
                 {
                     { typeof(string), true },
                     { typeof(Uri), true },
-                    { typeof(InstanceDescriptor), true },
+                    { typeof(InstanceDescriptor), true }
                 },
                 UriTypeConverterTests.s_converter);
         }
index 9e8ec3c..2a37821 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.ComponentModel.Design.Serialization;
 using Xunit;
 
 namespace System.ComponentModel.Tests
@@ -13,10 +14,11 @@ namespace System.ComponentModel.Tests
         [Fact]
         public static void CanConvertFrom_WithContext()
         {
-            CanConvertFrom_WithContext(new object[2, 2]
+            CanConvertFrom_WithContext(new object[3, 2]
                 {
                     { typeof(string), true },
-                    { typeof(Version), true }
+                    { typeof(Version), true },
+                    { typeof(InstanceDescriptor), true }
                 },
                 VersionConverterTests.s_converter);
         }
@@ -35,6 +37,18 @@ namespace System.ComponentModel.Tests
         }
 
         [Fact]
+        public static void CanConvertTo_WithContext()
+        {
+            CanConvertTo_WithContext(new object[3, 2]
+                {
+                    { typeof(string), true },
+                    { typeof(Version), true },
+                    { typeof(InstanceDescriptor), true }
+                },
+                VersionConverterTests.s_converter);
+        }
+
+        [Fact]
         public static void ConvertFromNull_WithContext_ThrowsNotSupportedException()
         {
             Assert.Throws<NotSupportedException>(