如何检查编程的托管程序集是否是86,64或值为anycpu? [英] How to check programmatically whether a managed assembly is x86, x64 or AnyCPU?
问题描述
我需要确定编程程序集是否是86,64或值为anycpu?有几乎相同的<一href="http://stackoverflow.com/questions/270531/how-to-determine-if-a-net-assembly-was-built-for-x86-or-x64">question,但是该解决方案,它提供了
I need to determine programmatically whether an assembly is x86, x64 or AnyCPU? There is an almost identical question, but the solution that it provides
Assembly assembly = Assembly.LoadFrom(fileName);
PortableExecutableKinds peKind;
ImageFileMachine imageFileMachine;
assembly.ManifestModule.GetPEKind(out peKind, out imageFileMachine);
试图从32位进程(反之亦然)加载64位汇编时会失败。
fails when trying to load a 64-bit assembly from a 32-bit process (and vice versa).
是否有编程方式找出程序集的编译类型万无一失的方法是什么?
Is there a foolproof way of programmatically finding out the compilation type of an assembly?
编辑:基于@BenVoigt的建议,我创建了一个<一个href="http://www.angryhacker.com/blog/archive/2012/03/03/how-to-determine-whether-an-assembly-is-32-bit-64.aspx"相对=nofollow>小的命令行实用程序来检查DLL是否被托管与否,以及是否其在x86 / x64 /值为anycpu。我希望有人认为它有用。
Based on @BenVoigt suggestion, I created a small command line utility that checks whether the DLL is managed or not and whether its x86/x64/AnyCPU. I hope someone finds it useful.
推荐答案
这个问题一直已覆盖:
- <一个href="http://stackoverflow.com/questions/1591557/how-to-tell-if-a-net-assembly-was-compiled-as-x86-x64-or-any-cpu">How判断一个.NET程序集编译为86,64或任何CPU
- <一个href="http://stackoverflow.com/questions/270531/how-to-determine-if-a-net-assembly-was-built-for-x86-or-x64">How以确定是否.NET程序集是专为x86或x64?
- <一个href="http://stackoverflow.com/questions/4186159/how-can-i-get-the-processor-architecture-of-an-assembly-dll">How我可以得到一个程序集DLL的处理器架构?
- 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
。这是一个可怕的想法,因为它将运行从组装code,此外,如果失败的位数不匹配的过程。
But the answers are incomplete, suggesting use of Assembly.LoadFrom
. That's a terrible idea, since it will run code from the assembly, in addition to failing if the bitness doesn't match your process.
相反,你应该使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.reflectiononlyloadfrom.aspx"><$c$c>Assembly.ReflectionOnlyLoadFrom$c$c>.这使您可以读取的元数据,而无需实际加载任何code,因此没有必要为架构是正确的。
Instead, you should use Assembly.ReflectionOnlyLoadFrom
. This lets you read the metadata without actually loading any code, and therefore there's no need for the architecture to be correct.
这篇关于如何检查编程的托管程序集是否是86,64或值为anycpu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!