c# - 引用的程序集返回 None 作为 ProcessorArchitecture

标签 c# .net reflection 64-bit cpu-architecture

我有一个 Web 项目,我使用 Assembly.ReflectionOnlyLoadFrom(filename) 加载 DLL。 。然后我调用assembly.GetReferencedAssemblies(); .

返回的AssemblyName都有ProcessorArchitecture设置为None .

主要 DLL 的 ProcessorArchitecture 是 x64,而 AnyCPU 和 x64 之间的引用有所不同。

知道为什么我无法获取这些引用程序集的 ProcessorArchitecture 吗?

更新:我刚刚看到这个 link说明:

Beginning with the .NET Framework 4, this property always returns ProcessorArchitecture.None for reference assemblies.

还有其他方法可以获取此信息吗?

最佳答案

我遇到了这个问题;我最终使用的代码如下所示:

static void Main() {
    // Load assembly. This can either be by name, or by calling GetReferencedAssemblies().
    Assembly referencedAssembly = Assembly.Load("AssemblyName");

    // Get the PEKind for the assembly, and handle appropriately
    PortableExecutableKinds referenceKind = GetPEKinds(referencedAssembly);
    if((referenceKind & PortableExecutableKinds.Required32Bit) > 0) {
        // is 32 bit assembly
    }
    else if((referenceKind & PortableExecutableKinds.PE32Plus) > 0) {
        // is 64 bit assembly
    }
    else if((referenceKind & PortableExecutableKinds.ILOnly) > 0) {
        // is AnyCpu
    }
}

static PortableExecutableKinds GetPEKinds(Assembly assembly) {
    PortableExecutableKinds peKinds;
    ImageFileMachine imageFileMachine;
    assembly.GetModules()[0].GetPEKind(out peKinds, out imageFileMachine);
    return peKinds;
}

关于c# - 引用的程序集返回 None 作为 ProcessorArchitecture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945117/

相关文章:

c# - 手动处理 WinForms 控件的滚动

java - 如何禁用 Java 安全管理器?

c# - unlockbits、lockbits 和 try-finally

c# - C#中如何从数据表中逐行读取数据

c# - 从连接到远程 MySQL 数据库的 PHP 服务检索/获取数据以显示到我的 ASP.Net Web 应用程序

c# - .net framework 4.0 的 File.ReadLines(..) 方法中的错误

.net - 如果、IIf() 和 If()

c++ - 我可以以 constexpr 方式获取 C++ 类型名称吗?

scala - 包装 isInstanceOf[] 调用的正确方法是什么?

c# - 创建一个接受文件(Stream)的网络服务不需要其他参数