提交 f4283ffb 编写于 作者: T Tom Meschter

Add a method to check that MVIDs match

Add a utility method to check that MVIDs match.

Given a path to an assembly and a loaded `Assembly` object,
`AssemblyUtilities.MvidsMatch` returns true if their MVIDs (Module
Version IDs) match, and false otherwise.

This commit contains the code itself and unit tests; a later commit will
wire this into the compilers/VS.
上级 526b85d2
......@@ -2,7 +2,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Roslyn.Test.Utilities;
......@@ -71,5 +73,34 @@ public void FindAssemblySet_TransitiveDependencies()
Assert.Contains(gammaDll.Path, results, StringComparer.OrdinalIgnoreCase);
Assert.Contains(deltaDll.Path, results, StringComparer.OrdinalIgnoreCase);
}
[Fact]
public void MvidsMatch_True()
{
var directory = Temp.CreateDirectory();
var alphaDll = directory.CreateFile("Alpha.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Alpha);
var assembly = Assembly.Load(File.ReadAllBytes(alphaDll.Path));
var result = AssemblyUtilities.MvidsMatch(alphaDll.Path, assembly);
Assert.True(result);
}
[Fact]
public void MvidsMatch_False()
{
var directory = Temp.CreateDirectory();
var alphaDll = directory.CreateFile("Alpha.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Alpha);
var betaDll = directory.CreateFile("Beta.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Beta);
var assembly = Assembly.Load(File.ReadAllBytes(betaDll.Path));
var result = AssemblyUtilities.MvidsMatch(alphaDll.Path, assembly);
Assert.False(result);
}
}
}
......@@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
......@@ -24,6 +25,11 @@ internal static class AssemblyUtilities
/// </remarks>
public static ImmutableArray<string> FindAssemblySet(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
Queue<string> workList = new Queue<string>();
HashSet<string> assemblySet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
......@@ -60,5 +66,31 @@ public static ImmutableArray<string> FindAssemblySet(string filePath)
return ImmutableArray.CreateRange(assemblySet);
}
/// <summary>
/// Given a path to an assembly and a loaded <see cref="Assembly"/>, returns
/// true if their MVIDs match, and false otherwise.
/// </summary>
public static bool MvidsMatch(string filePath, Assembly assembly)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
using (var reader = new PEReader(FileUtilities.OpenRead(filePath)))
{
var metadataReader = reader.GetMetadataReader();
var mvidHandle = metadataReader.GetModuleDefinition().Mvid;
var fileMvid = metadataReader.GetGuid(mvidHandle);
return fileMvid == assembly.ManifestModule.ModuleVersionId;
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册