From f43b0c124c2018b7b0fa8be0e173280ebc8031c7 Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Thu, 3 Aug 2023 12:22:53 -0700 Subject: [PATCH] Pass through ComWrappers based objects to COM type descriptor (#89887) WinForms hosts the the COM type descriptor. It has been updated to work with ComWrappers based objects- adding the check for them in TypeDescriptor. There are hard dependencies on Windows in the current implementation. We can potentially update to conditionalize these. https://github.com/dotnet/winforms/issues/9291 tracks. There is no automated test as it would require adding a dependency upstream to WinForms. --- .../src/System/ComponentModel/TypeDescriptor.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs index b08f16cd9ae34..874fa6e2e7d03 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs @@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; +using System.Runtime.InteropServices; using System.Threading; namespace System.ComponentModel @@ -1541,6 +1542,17 @@ private static TypeDescriptionNode NodeFor(object instance, bool createDelegator { type = ComObjectType; } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + && ComWrappers.TryGetComInstance(instance, out nint unknown)) + { + // ComObjectType uses the Windows Forms provided ComNativeDescriptor. It currently has hard Win32 + // API dependencies. Even though ComWrappers work with other platforms, restricting to Windows until + // such time that the ComNativeDescriptor can handle basic COM types on other platforms. + // + // Tracked with https://github.com/dotnet/winforms/issues/9291 + Marshal.Release(unknown); + type = ComObjectType; + } if (createDelegator) {