From 10393999d85eb12cd7ffe73b1437c54c440cff00 Mon Sep 17 00:00:00 2001 From: Andy Gerlicher Date: Thu, 5 Apr 2018 12:04:12 -0700 Subject: [PATCH] Check PublicKeyToken before resolving an assembly * If there is a request for one of our assemblies with another key do not handle the AssemblyResolve event to let other resolvers have a chance. Closes #23 --- src/MSBuildLocator/MSBuildLocator.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index 8c6b68e4..d34a79dd 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -6,11 +6,14 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Text; namespace Microsoft.Build.Locator { public static class MSBuildLocator { + private const string MSBuildPublicKeyToken = "b03f5f7f11d50a3a"; + private static readonly string[] s_msBuildAssemblies = { "Microsoft.Build", "Microsoft.Build.Framework", "Microsoft.Build.Tasks.Core", @@ -101,7 +104,24 @@ private static bool IsMSBuildAssembly(Assembly assembly) private static bool IsMSBuildAssembly(AssemblyName assemblyName) { - return s_msBuildAssemblies.Contains(assemblyName.Name, StringComparer.OrdinalIgnoreCase); + if (!s_msBuildAssemblies.Contains(assemblyName.Name, StringComparer.OrdinalIgnoreCase)) + { + return false; + } + + var publicKeyToken = assemblyName.GetPublicKeyToken(); + if (publicKeyToken == null || publicKeyToken.Length == 0) + { + return false; + } + + var sb = new StringBuilder(); + foreach (var b in publicKeyToken) + { + sb.Append($"{b:x2}"); + } + + return sb.ToString().Equals(MSBuildPublicKeyToken, StringComparison.OrdinalIgnoreCase); } private static IEnumerable GetInstances()