This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement System.ComponentModel.VersionConverter (#28516)
* Implement System.ComponentModel.VersionConverter VersionConverter is a new System.ComponentModel.TypeConverter subclass that handle conversions between string and System.Version. * Fix tests build for VersionTypeConverter * Use Version.Parse() instead of new Version() to save an allocation * Add test data to ensure that the version string is trimmed * Call base class implementation instead of explicitly throwing * Always throw a FormatException when a version string is invalid
- Loading branch information
Showing
9 changed files
with
221 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/System.ComponentModel.TypeConverter/src/System/ComponentModel/VersionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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.Globalization; | ||
|
||
namespace System.ComponentModel | ||
{ | ||
/// <summary> | ||
/// <para>Provides a type converter to convert Version objects to and | ||
/// from various other representations.</para> | ||
/// </summary> | ||
public class VersionConverter : TypeConverter | ||
{ | ||
/// <summary> | ||
/// <para>Gets a value indicating whether this converter can convert an object in the | ||
/// given source type to a Version.</para> | ||
/// </summary> | ||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||
{ | ||
if (sourceType == null) | ||
throw new ArgumentNullException(nameof(sourceType)); | ||
|
||
return sourceType == typeof(string) || sourceType == typeof(Version); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Gets a value indicating whether this converter can | ||
/// convert an object to the given destination type using the context.</para> | ||
/// </summary> | ||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) | ||
{ | ||
return destinationType == typeof(string) || destinationType == typeof(Version); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Converts the given object to a Version.</para> | ||
/// </summary> | ||
/// <exception cref="FormatException"><paramref name="value"/> is not a valid version string</exception> | ||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
{ | ||
if (value is string versionString) | ||
{ | ||
try | ||
{ | ||
return Version.Parse(versionString); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, versionString, nameof(Version)), e); | ||
} | ||
} | ||
|
||
if (value is Version version) | ||
{ | ||
return new Version(version.Major, version.Minor, version.Build, version.Revision); // return new instance | ||
} | ||
|
||
return base.ConvertFrom(context, culture, value); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Converts the given value object to | ||
/// the specified destination type using the specified context and arguments.</para> | ||
/// </summary> | ||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) | ||
{ | ||
if (destinationType == null) | ||
{ | ||
throw new ArgumentNullException(nameof(destinationType)); | ||
} | ||
|
||
if (value is Version version) | ||
{ | ||
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); | ||
} | ||
|
||
public override bool IsValid(ITypeDescriptorContext context, object value) | ||
{ | ||
if (value is string version) | ||
{ | ||
return Version.TryParse(version, out Version _); | ||
} | ||
return value is Version; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/System.ComponentModel.TypeConverter/tests/TypeDescriptorTests.netcoreapp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Xunit; | ||
|
||
namespace System.ComponentModel.Tests | ||
{ | ||
public partial class TypeDescriptorTests | ||
{ | ||
[Theory] | ||
[InlineData(typeof(Version), typeof(VersionConverter))] | ||
public static void GetConverter_NetCoreApp(Type targetType, Type resultConverterType) => | ||
GetConverter(targetType, resultConverterType); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/System.ComponentModel.TypeConverter/tests/VersionConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Xunit; | ||
|
||
namespace System.ComponentModel.Tests | ||
{ | ||
public class VersionConverterTests : ConverterTestBase | ||
{ | ||
private static VersionConverter s_converter = new VersionConverter(); | ||
|
||
[Fact] | ||
public static void CanConvertFrom_WithContext() | ||
{ | ||
CanConvertFrom_WithContext(new object[2, 2] | ||
{ | ||
{ typeof(string), true }, | ||
{ typeof(Version), true } | ||
}, | ||
VersionConverterTests.s_converter); | ||
} | ||
|
||
[Fact] | ||
public static void ConvertFrom_WithContext() | ||
{ | ||
ConvertFrom_WithContext(new object[4, 3] | ||
{ | ||
{"1.2", new Version(1, 2), null}, | ||
{"1.2.3", new Version(1, 2, 3), null}, | ||
{"1.2.3.4", new Version(1, 2, 3, 4), null}, | ||
{" 1.2.3.4 ", new Version(1, 2, 3, 4), null} | ||
}, | ||
VersionConverterTests.s_converter); | ||
} | ||
|
||
[Fact] | ||
public static void ConvertFromNull_WithContext_ThrowsNotSupportedException() | ||
{ | ||
Assert.Throws<NotSupportedException>( | ||
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, null)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("1")] | ||
[InlineData("1.-2")] | ||
[InlineData("1.9999999999")] | ||
public static void ConvertFromInvalidVersion_WithContext_ThrowsFormatException(string version) | ||
{ | ||
Assert.Throws<FormatException>( | ||
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, version)); | ||
} | ||
} | ||
} |