[Bugfix] Allows the user to return null (default value) when the return value type is a reference type.

This commit is contained in:
DingpingZhang committed 2019-04-02 11:47:39 +08:00
1 parent 82e9577a9f
commit 19d6d1c289
1 file changed
+11 -6
+11 -6
View File
@@ -56,12 +56,17 @@ namespace HandyControl.Controls
if (!(frameworkElement.DataContext is IDialogResult dialogResult))
throw new InvalidOperationException("The view model of the dialog is not implement the IDialogResult interface. ");
if (!(dialogResult.Result is TResult result))
throw new InvalidCastException("Could not cast " +
$"the {(dialogResult.Result != null ? $"{dialogResult.Result.GetType()} type" : "null value")} " +
$"to the {typeof(TResult)} type. ");
return result;
switch (dialogResult.Result)
{
case TResult result:
return result;
case null when !typeof(TResult).IsValueType:
return default(TResult); // The default value of the value type may cause confusion, so should not help the user decide.
default:
throw new InvalidCastException("Could not cast " +
$"the {(dialogResult.Result != null ? $"{dialogResult.Result.GetType()} type" : "null value")} " +
$"to the {typeof(TResult)} type. ");
}
}
}