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
VersionConverter is a new System.ComponentModel.TypeConverter subclass that handle conversions between string and System.Version.
- Loading branch information
Showing
8 changed files
with
148 additions
and
0 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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
// 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> | ||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
{ | ||
if (value is string versionString) | ||
{ | ||
// Let the Version constructor throw any informative exceptions | ||
return new Version(versionString); | ||
} | ||
|
||
if (value is Version version) | ||
{ | ||
return new Version(version.Major, version.Minor, version.Build, version.Revision); // return new instance | ||
} | ||
|
||
throw GetConvertFromException(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); | ||
} | ||
|
||
throw GetConvertToException(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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
// 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[3, 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} | ||
}, | ||
VersionConverterTests.s_converter); | ||
} | ||
|
||
[Fact] | ||
public static void ConvertFrom_WithContext_Negative() | ||
{ | ||
Assert.Throws<NotSupportedException>( | ||
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, null)); | ||
Assert.Throws<ArgumentException>( | ||
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "")); | ||
Assert.Throws<ArgumentException>( | ||
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "1")); | ||
} | ||
} | ||
} |