From 8ccaf89a903e8890cbf569856890d5f9debc5b85 Mon Sep 17 00:00:00 2001 From: Regenhardt Marlon Date: Tue, 19 Dec 2023 18:09:14 +0100 Subject: [PATCH] JSInterop: Enable returning null for a nullable value type When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value. #30366 --- .../src/Infrastructure/TaskGenericsUtil.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/JSInterop/Microsoft.JSInterop/src/Infrastructure/TaskGenericsUtil.cs b/src/JSInterop/Microsoft.JSInterop/src/Infrastructure/TaskGenericsUtil.cs index a03d87536db1..ee99390a5d89 100644 --- a/src/JSInterop/Microsoft.JSInterop/src/Infrastructure/TaskGenericsUtil.cs +++ b/src/JSInterop/Microsoft.JSInterop/src/Infrastructure/TaskGenericsUtil.cs @@ -89,7 +89,9 @@ public void SetResult(object tcs, object? result) // If necessary, attempt a cast var typedResult = result is T resultT ? resultT - : (T)Convert.ChangeType(result, typeof(T), CultureInfo.InvariantCulture)!; + : result == null && default(T) == null + ? default(T) + : (T)Convert.ChangeType(result, typeof(T), CultureInfo.InvariantCulture)!; typedTcs.SetResult(typedResult!); }