如何以编程方式检查托管程序集是 x86、x64 还是 AnyCPU?

编程入门 行业动态 更新时间:2024-10-11 21:20:45
本文介绍了如何以编程方式检查托管程序集是 x86、x64 还是 AnyCPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要以编程方式确定程序集是 x86、x64 还是 AnyCPU?有一个几乎相同的 问题,但它提供的解决方案

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 的建议,我创建了一个 小型命令行实用程序,用于检查 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.

推荐答案

这个问题已经讨论过了:

This question's been covered already:

  • 如何判断 .NET 程序集是否编译为 x86、x64 或任何 CPU
  • 如何确定.NET 程序集是为 x86 还是 x64 构建的?
  • 如何获取处理器架构一个程序集 dll?

但答案不完整,建议使用Assembly.LoadFrom.这是一个糟糕的主意,因为它会运行程序集中的代码,而且如果位数与您的进程不匹配,则会失败.

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.

相反,您应该使用 Assembly.ReflectionOnlyLoadFrom.这让您无需实际加载任何代码即可读取元数据,因此架构无需正确.

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.

更多推荐

如何以编程方式检查托管程序集是 x86、x64 还是 AnyCPU?

本文发布于:2023-11-02 12:12:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   程序   AnyCPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!