Skip to content

Commit e3fe539

Browse files
Remove ValueType support and reflection piece from TaskResultConverter
Update description to explicitly call out returning null instead of default if no task or task result not ready.
1 parent 1398210 commit e3fe539

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

Microsoft.Toolkit.Uwp.UI/Converters/TaskResultConverter.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,21 @@ namespace Microsoft.Toolkit.Uwp.UI.Converters
1414
/// This is needed because accessing <see cref="Task{TResult}.Result"/> when the task has not
1515
/// completed yet will block the current thread and might cause a deadlock (eg. if the task was
1616
/// scheduled on the same synchronization context where the result is being retrieved from).
17-
/// The methods in this converter will safely return <see langword="default"/> if the input
17+
/// The methods in this converter will safely return <see langword="null"/> if the input
1818
/// task is not set yet, still running, has faulted, or has been canceled.
1919
/// </summary>
20-
/// <seealso href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/default-values">Default values of C# types</seealso>
2120
public sealed class TaskResultConverter : IValueConverter
2221
{
2322
/// <inheritdoc/>
2423
public object Convert(object value, Type targetType, object parameter, string language)
2524
{
26-
//// Check if we need to return a specific type, which only matters for value types where the default won't be null.
27-
var hasValueTypeTarget = targetType is not null && targetType.IsValueType;
28-
//// If we have a value type, then calculate it's default value to return, as we probably need it unless the task is completed.
29-
var defaultValue = hasValueTypeTarget ? Activator.CreateInstance(targetType) : null;
30-
3125
if (value is Task task)
3226
{
33-
// If we have a task, check if we have a result now, otherwise the non-generic version of this
34-
// function always returns null, so we want to use whatever we actually want the default value to be
35-
// for our target type (in case it's a value type).
36-
return task.GetResultOrDefault() ?? defaultValue;
27+
return task.GetResultOrDefault();
3728
}
3829
else if (value is null)
3930
{
40-
// If we have a value type, return that value, otherwise this will be null.
41-
return defaultValue;
31+
return null;
4232
}
4333

4434
// Otherwise, we'll just pass through whatever value/result was given to us.

UnitTests/UnitTests.UWP/Converters/Test_TaskResultConverter.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Test_TaskResultConverter
1717
{
1818
[TestCategory("Converters")]
1919
[UITestMethod]
20+
[Ignore] // Ignore this value type test. Behavior will return null currently and not default.
2021
public void Test_TaskResultConverter_Instance_Int32()
2122
{
2223
TaskResultConverter converter = new();
@@ -92,9 +93,15 @@ public void Test_TaskResultConverter_Instance_NullObject()
9293

9394
Assert.AreEqual(null, converter.Convert(null, null, null, null));
9495

95-
Assert.AreEqual(0, (int)converter.Convert(null, typeof(int), null, null));
96+
// TODO: Think there may still be a problem for value types in x:Bind expressions, represented by these tests here,
97+
// but was going to be too big a change for 7.1.3, will have to get more feedback and evaluate later.
98+
/*Assert.AreEqual(0, (int)converter.Convert(null, typeof(int), null, null));
9699
97-
Assert.AreEqual(false, (bool)converter.Convert(null, typeof(bool), null, null));
100+
Assert.AreEqual(false, (bool)converter.Convert(null, typeof(bool), null, null));*/
101+
102+
Assert.AreEqual(null, converter.Convert(null, typeof(int), null, null));
103+
104+
Assert.AreEqual(null, converter.Convert(null, typeof(bool), null, null));
98105

99106
Assert.AreEqual(null, (int?)converter.Convert(null, typeof(int?), null, null));
100107

0 commit comments

Comments
 (0)