Browse Source
Merge pull request #3585 from AvaloniaUI/fixes/3584-convert-timespan
Use TypeConverters in TypeUtilities.TryConvert
pull/3594/head
Jumar Macato
7 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
21 additions and
0 deletions
-
src/Avalonia.Base/Utilities/TypeUtilities.cs
-
tests/Avalonia.Base.UnitTests/Data/DefaultValueConverterTests.cs
|
|
|
@ -2,6 +2,7 @@ |
|
|
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.ComponentModel; |
|
|
|
using System.Globalization; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
@ -185,6 +186,14 @@ namespace Avalonia.Utilities |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var typeConverter = TypeDescriptor.GetConverter(to); |
|
|
|
|
|
|
|
if (typeConverter.CanConvertFrom(from) == true) |
|
|
|
{ |
|
|
|
result = typeConverter.ConvertFrom(null, culture, value); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
var cast = FindTypeConversionOperatorMethod(from, to, OperatorType.Implicit | OperatorType.Explicit); |
|
|
|
|
|
|
|
if (cast != null) |
|
|
|
|
|
|
|
@ -50,6 +50,18 @@ namespace Avalonia.Base.UnitTests.Data.Converters |
|
|
|
Assert.Equal(TestEnum.Bar, result); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Can_Convert_String_To_TimeSpan() |
|
|
|
{ |
|
|
|
var result = DefaultValueConverter.Instance.Convert( |
|
|
|
"00:00:10", |
|
|
|
typeof(TimeSpan), |
|
|
|
null, |
|
|
|
CultureInfo.InvariantCulture); |
|
|
|
|
|
|
|
Assert.Equal(TimeSpan.FromSeconds(10), result); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Can_Convert_Int_To_Enum() |
|
|
|
{ |
|
|
|
|