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 5k
Implement System.ComponentModel.VersionConverter #28516
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0256728
Implement System.ComponentModel.VersionConverter
0xced a844602
Fix tests build for VersionTypeConverter
stephentoub 09fe984
Use Version.Parse() instead of new Version() to save an allocation
0xced 10cdee3
Add test data to ensure that the version string is trimmed
0xced 2ba1f61
Call base class implementation instead of explicitly throwing
0xced 004ef11
Always throw a FormatException when a version string is invalid
0xced File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure would be nice if every type listed here had a comment on the class itself or in docs someplace that stated it had a type converter. The first place I looked when serialization broke for me on the upgrade to 3.1 from 2.2 was on the Version class and I didn't see it. I searched for usages on GitHub and it's parser must have problems with
[typeof(Version)]
as I searched fortypeof(Version)
andtypeof(System.Version)
with no luck. JamesNK/Newtonsoft.Json#2243