From c5cfbbfd3a245e7e941c0b9005a64e2d03cdf469 Mon Sep 17 00:00:00 2001 From: "Ryland 41491307+ryalanms@users.noreply.github.com" <41491307+ryalanms@users.noreply.github.com> Date: Thu, 22 Aug 2019 13:52:03 -0700 Subject: [PATCH] Remove PKT for mscorlib to allow PathAssemblyResolver to return most recent assembly version --- .../PresentationBuildTasks.csproj | 3 ++ .../System/Windows/Markup/ReflectionHelper.cs | 2 +- .../RetargetablePathAssemblyResolver.cs | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RetargetablePathAssemblyResolver.cs diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/PresentationBuildTasks.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/PresentationBuildTasks.csproj index 61dbb1e7..37b21df0 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/PresentationBuildTasks.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/PresentationBuildTasks.csproj @@ -98,6 +98,9 @@ Shared\System\Windows\Markup\ReflectionHelper.cs + + Shared\System\Windows\Markup\RetargetablePathAssemblyResolver.cs + Shared\MS\Internal\Xaml\Parser\SpecialBracketCharacters.cs diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/ReflectionHelper.cs b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/ReflectionHelper.cs index d2d53646..b40c2c4c 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/ReflectionHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/ReflectionHelper.cs @@ -62,7 +62,7 @@ namespace System.Xaml // System.Reflection.MetadataLoadContext Assembly cache _cachedMetadataLoadContextAssemblies = new Dictionary(StringComparer.OrdinalIgnoreCase); _cachedMetadataLoadContextAssembliesByNameNoExtension = new Dictionary(StringComparer.OrdinalIgnoreCase); - _metadataLoadContext = new MetadataLoadContext(new PathAssemblyResolver(assemblyPaths), MscorlibReflectionAssemblyName); + _metadataLoadContext = new MetadataLoadContext(new RetargetablePathAssemblyResolver(assemblyPaths), MscorlibReflectionAssemblyName); _localAssemblyName = string.Empty; } diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RetargetablePathAssemblyResolver.cs b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RetargetablePathAssemblyResolver.cs new file mode 100644 index 00000000..b35d218e --- /dev/null +++ b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RetargetablePathAssemblyResolver.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.IO; +using System.Reflection; + +namespace MS.Internal.Markup +{ + public class RetargetablePathAssemblyResolver : MetadataAssemblyResolver + { + private PathAssemblyResolver _pathAssemblyResolver; + + public RetargetablePathAssemblyResolver(IEnumerable assemblyPaths) + { + _pathAssemblyResolver = new PathAssemblyResolver(assemblyPaths); + } + + public override Assembly Resolve(MetadataLoadContext context, AssemblyName assemblyName) + { + // PathAssemblyResolver will resolve the target assembly to the highest + // version of the target assembly available in the 'assemblyPaths' assembly + // list, only if the public key token for the target assembly is not set. + // Remove the public key token from 'mscorlib' to allow PathAssemblyResolver + // to resolve the most recent version of 'mscorlib'. + if (assemblyName.Name.Equals(ReflectionHelper.MscorlibReflectionAssemblyName)) + { + assemblyName.SetPublicKeyToken(null); + } + + return _pathAssemblyResolver.Resolve(context, assemblyName); + } + } +}