diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs index a5751f5031..f3d0b2afbc 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/AppDomainUtilities.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. #if NETFRAMEWORK @@ -6,7 +6,6 @@ using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; -using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities; @@ -18,9 +17,22 @@ internal static class AppDomainUtilities { private const string ObjectModelVersionBuiltAgainst = "11.0.0.0"; + private const string ObjectModelAssemblyName = "Microsoft.VisualStudio.TestPlatform.ObjectModel"; + private static readonly Version DefaultVersion = new(); private static readonly Version Version45 = new("4.5"); + /// + /// Resolves the loaded VSTest object-model assembly by simple name, so this AppDomain-wiring code does not + /// need a compile-time reference to it. By the time these methods run (test source host setup during + /// discovery/execution) the adapter has already loaded the object model into the current (parent) domain, + /// so its identity — including any binding redirect in effect — matches what a direct type reference resolved to. + /// + /// The object-model assembly. + private static Assembly GetObjectModelAssembly() + => AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => string.Equals(a.GetName().Name, ObjectModelAssemblyName, StringComparison.Ordinal)) + ?? Assembly.Load(ObjectModelAssemblyName); + /// /// Gets or sets the Xml Utilities instance. /// @@ -90,7 +102,7 @@ internal static string GetTargetFrameworkVersionString(string testSourcePath) var resolutionPaths = new List { - Path.GetDirectoryName(typeof(TestCase).Assembly.Location), + Path.GetDirectoryName(GetObjectModelAssembly().Location), Path.GetDirectoryName(testSourcePath), }; @@ -157,10 +169,10 @@ internal static void SetConfigurationFile(AppDomainSetup appDomainSetup, string? try { // Add redirection of the built 11.0 Object Model assembly to the current version if that is not 11.0 - string currentVersionOfObjectModel = typeof(TestCase).Assembly.GetName().Version.ToString(); + string currentVersionOfObjectModel = GetObjectModelAssembly().GetName().Version.ToString(); if (!string.Equals(currentVersionOfObjectModel, ObjectModelVersionBuiltAgainst, StringComparison.Ordinal)) { - AssemblyName assemblyName = typeof(TestCase).Assembly.GetName(); + AssemblyName assemblyName = GetObjectModelAssembly().GetName(); byte[] configurationBytes = XmlUtilities.AddAssemblyRedirection( testSourceConfigFile,