From ea94d81609799a2af7189033ddd9da80fcd059fb Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 28 Apr 2026 03:17:02 -0700 Subject: [PATCH] Throw ObjectDisposedException when PlatformImpl is null in HeadlessExtension (#21276) --- .../Avalonia.Headless/HeadlessWindowExtensions.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Headless/Avalonia.Headless/HeadlessWindowExtensions.cs b/src/Headless/Avalonia.Headless/HeadlessWindowExtensions.cs index 78b89d6cb1..8bb3bf4f34 100644 --- a/src/Headless/Avalonia.Headless/HeadlessWindowExtensions.cs +++ b/src/Headless/Avalonia.Headless/HeadlessWindowExtensions.cs @@ -153,7 +153,11 @@ public static class HeadlessWindowExtensions private static IHeadlessWindow GetImpl(this TopLevel topLevel) { - return topLevel.PlatformImpl as IHeadlessWindow ?? - throw new InvalidOperationException("TopLevel must be a headless window."); + return topLevel.PlatformImpl switch + { + null => throw new ObjectDisposedException(topLevel.GetType().Name), + IHeadlessWindow headless => headless, + _ => throw new InvalidOperationException("TopLevel must be a headless window.") + }; } }