Retrieve MVID of an assembly from c#?
How to ret开发者_如何学Pythonrieve the module version identifier (MVID) of a .NET assembly using reflection in c#?
Should be:
var myAssembly = Assembly.GetExecutingAssembly(); //or whatever
var mvid = myAssembly.ManifestModule.ModuleVersionID;
There can be other modules in an assembly, but the ManifestModule would be the one that "identifies" the assembly itself.
Here's a sample that doesn't use Reflection to load the assembly but instead uses System.Reflection.Metadata:
using (var stream = File.OpenRead(filePath))
{
PEReader reader = new PEReader(stream);
var metadataReader = reader.GetMetadataReader();
var mvidHandle = metadataReader.GetModuleDefinition().Mvid;
var mvid = metadataReader.GetGuid(mvidHandle);
}
And here's a sample of using Mono.Cecil:
var module = Mono.Cecil.ModuleDefinition.ReadModule(filePath);
var mvid = module.Mvid;
And here's an example of a standalone code to read the MVID without any dependencies. It is a stripped-down version of Mono.Cecil in a single file: https://github.com/KirillOsenkov/MetadataTools/blob/master/src/PEFile/ImageReader.cs
精彩评论