diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
index a81acbe1c6218..8851446e6b2b5 100644
--- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
@@ -129,6 +129,21 @@ private static bool GetLinqExpressionsBuiltWithIsInterpretingOnly()
public static bool IsInContainer => GetIsInContainer();
public static bool SupportsComInterop => IsWindows && IsNotMonoRuntime && !IsNativeAot; // matches definitions in clr.featuredefines.props
+
+#if NETCOREAPP
+ public static bool IsBuiltInComEnabled => SupportsComInterop
+ && (AppContext.TryGetSwitch("System.Runtime.InteropServices.BuiltInComInterop.IsSupported", out bool isEnabled)
+ ? isEnabled
+ : true);
+#else
+ public static bool IsBuiltInComEnabled => SupportsComInterop;
+#endif
+
+ // Automation refers to OLE Automation support. Automation support here means the OS
+ // and runtime provide support for the following: IDispatch, STA apartments, etc. This
+ // is typically available whenever COM support is enabled, but Windows Nano Server is an exception.
+ public static bool IsBuiltInComEnabledWithOSAutomationSupport => IsBuiltInComEnabled && IsNotWindowsNanoServer;
+
public static bool SupportsSsl3 => GetSsl3Support();
public static bool SupportsSsl2 => IsWindows && !PlatformDetection.IsWindows10Version1607OrGreater;
diff --git a/src/libraries/System.Runtime.InteropServices/src/System.Runtime.InteropServices.csproj b/src/libraries/System.Runtime.InteropServices/src/System.Runtime.InteropServices.csproj
index ad86c57c9f904..fc2048c219ac9 100644
--- a/src/libraries/System.Runtime.InteropServices/src/System.Runtime.InteropServices.csproj
+++ b/src/libraries/System.Runtime.InteropServices/src/System.Runtime.InteropServices.csproj
@@ -37,7 +37,6 @@
-
diff --git a/src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/SetWin32ContextInIDispatchAttribute.cs b/src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/SetWin32ContextInIDispatchAttribute.cs
deleted file mode 100644
index 6d214badb2879..0000000000000
--- a/src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/SetWin32ContextInIDispatchAttribute.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.InteropServices
-{
- [Obsolete("SetWin32ContextInIDispatchAttribute has been deprecated. Application Domains no longer respect Activation Context boundaries in IDispatch calls.")]
- [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
- public sealed class SetWin32ContextInIDispatchAttribute : Attribute
- {
- public SetWin32ContextInIDispatchAttribute()
- {
- }
- }
-}
diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj
index d39b569ec036f..34964fb33f2ab 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj
+++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System.Runtime.InteropServices.Tests.csproj
@@ -159,7 +159,6 @@
-
diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComAwareEventInfoTests.Windows.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComAwareEventInfoTests.Windows.cs
index 871ada5dfc06a..54a58cfacdb98 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComAwareEventInfoTests.Windows.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComAwareEventInfoTests.Windows.cs
@@ -22,7 +22,7 @@ public class DispAttributeClass
public void Event() { }
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_ComObjectWithoutComEventInterfaceAttribute_ThrowsInvalidOperationException()
{
var attribute = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event));
@@ -33,7 +33,7 @@ public void AddEventHandler_ComObjectWithoutComEventInterfaceAttribute_ThrowsInv
Assert.Throws(() => attribute.RemoveEventHandler(target, handler));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_ComObjectWithMultipleComEventInterfaceAttribute_ThrowsAmbiguousMatchException()
{
// C# doesn't let us apply multiple ComEventInterface values, so RefEmit is necessary.
@@ -57,7 +57,7 @@ public void AddEventHandler_ComObjectWithMultipleComEventInterfaceAttribute_Thro
Assert.Throws(() => attribute.RemoveEventHandler(target, handler));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_NullSourceTypeEventInterface_ThrowsNullReferenceException()
{
var attribute = new ComAwareEventInfo(typeof(NullSourceType), nameof(NullSourceType.Event));
@@ -74,7 +74,7 @@ public interface NullSourceType
event EventHandler Event;
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_NoSuchSourceTypeEventInterface_ThrowsArgumentNullException()
{
var attribute = new ComAwareEventInfo(typeof(NoSuchSourceType), nameof(NoSuchSourceType.Event));
@@ -91,7 +91,7 @@ public interface NoSuchSourceType
event EventHandler Event;
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_NoDispIdAttribute_ThrowsInvalidOperationException()
{
var attribute = new ComAwareEventInfo(typeof(NoDispAttributeInterface), nameof(NoDispAttributeInterface.Event));
@@ -102,7 +102,7 @@ public void AddEventHandler_NoDispIdAttribute_ThrowsInvalidOperationException()
Assert.Throws(() => attribute.RemoveEventHandler(target, handler));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_TargetNotIConnectionIConnectionPointContainer_ThrowsInvalidCastException()
{
var attribute = new ComAwareEventInfo(typeof(DispAttributeInterface), nameof(DispAttributeInterface.Event));
diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComEventsHelperTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComEventsHelperTests.cs
index 1e9be2d697195..44ee17e16e20d 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComEventsHelperTests.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComEventsHelperTests.cs
@@ -14,15 +14,13 @@ public void Combine_Unix_ThrowsPlatformNotSupportedException()
Assert.Throws(() => ComEventsHelper.Combine(null, Guid.Empty, 1, null));
}
- [Fact]
- [PlatformSpecific(TestPlatforms.Windows)]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Combine_NullRcw_ThrowsArgumentNullException()
{
AssertExtensions.Throws(null, () => ComEventsHelper.Combine(null, Guid.Empty, 1, null));
}
- [Fact]
- [PlatformSpecific(TestPlatforms.Windows)]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Combine_NotComObject_ThrowsArgumentException()
{
AssertExtensions.Throws("obj", () => ComEventsHelper.Combine(1, Guid.Empty, 1, null));
@@ -35,15 +33,13 @@ public void Remove_Unix_ThrowPlatformNotSupportedException()
Assert.Throws(() => ComEventsHelper.Remove(null, Guid.Empty, 1, null));
}
- [Fact]
- [PlatformSpecific(TestPlatforms.Windows)]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Remove_NullRcw_ThrowsArgumentNullException()
{
AssertExtensions.Throws(null, () => ComEventsHelper.Remove(null, Guid.Empty, 1, null));
}
- [Fact]
- [PlatformSpecific(TestPlatforms.Windows)]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Remove_NotComObject_ThrowsArgumentException()
{
AssertExtensions.Throws("obj", () => ComEventsHelper.Remove(1, Guid.Empty, 1, null));
diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComVisibleAttributeTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComVisibleAttributeTests.cs
index 516c92ab87bfb..a7356312e602e 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComVisibleAttributeTests.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/ComVisibleAttributeTests.cs
@@ -8,7 +8,7 @@ namespace System.Runtime.InteropServices.Tests
[ComVisible(true)]
public class ComVisibleAttributeTests
{
- [Fact]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Exists()
{
Type type = typeof(ComVisibleAttributeTests);
@@ -16,7 +16,7 @@ public void Exists()
Assert.True(attribute.Value);
}
- [Theory]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
[InlineData(true)]
[InlineData(false)]
public void Ctor_Visible(bool visibility)
diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/DispatchWrapperTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/DispatchWrapperTests.cs
index 12211549fe006..4317cfa4c9b3e 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/DispatchWrapperTests.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/DispatchWrapperTests.cs
@@ -23,10 +23,9 @@ public void Ctor_NonNull_ThrowsPlatformNotSupportedException(object value)
Assert.Throws(() => new DispatchWrapper(value));
}
- [Theory]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
[InlineData("")]
[InlineData(0)]
- [PlatformSpecific(TestPlatforms.Windows)]
public void Ctor_NonDispatchObject_ThrowsInvalidCastException(object value)
{
Assert.Throws(() => new DispatchWrapper(value));
diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/Marshal/ChangeWrapperHandleStrengthTests.Windows.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/Marshal/ChangeWrapperHandleStrengthTests.Windows.cs
index 6579678cd309c..184308df8950c 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/Marshal/ChangeWrapperHandleStrengthTests.Windows.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/Marshal/ChangeWrapperHandleStrengthTests.Windows.cs
@@ -26,7 +26,7 @@ public static IEnumerable