我需要以编程方式确定程序集是x86,x64还是AnyCPU?有一个几乎相同的question,但它提供的解决方案
Assembly assembly = Assembly.LoadFrom(fileName);
PortableExecutableKinds peKind;
ImageFileMachine imageFileMachine;
assembly.ManifestModule.GetPEKind(out peKind, out imageFileMachine);
尝试从32位进程加载64位程序集时失败(反之亦然) .
是否有一种以编程方式查找程序集的编译类型的万无一失的方法?
编辑:基于@BenVoigt的建议,我创建了一个small command line utility来检查DLL是否被管理以及它是否是x86 / x64 / AnyCPU . 我希望有人发现它有用 .
1 回答
这个问题已被涵盖:
How to Tell if a .NET Assembly Was Compiled as x86, x64 or Any CPU
How to determine if a .NET assembly was built for x86 or x64?
How can I get the processor architecture of an assembly dll?
但答案不完整,建议使用
Assembly.LoadFrom
. 那's a terrible idea, since it will run code from the assembly, in addition to failing if the bitness doesn' t匹配你的过程 .相反,你应该使用Assembly.ReflectionOnlyLoadFrom . 这使您可以在不实际加载任何代码的情况下读取元数据,因此不需要架构正确 .