如何为 csi.exe 设置平台架构

How to set platform architecture for csi.exe

我在 运行 csi.exe 时看到以下错误:

System.BadImageFormatException: Could not load file or assembly 'xyz.dll' or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

在 Visual Studio 中编译会引发警告,但编译和运行没有问题:

There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "xyz", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.

看来我必须在 运行 csi.exe 时指定处理器架构?怎么做?

您的程序集 xyz.dll 在 32 位进程中仅编译为 运行。这可能是有充分理由的(例如加载另一个仅适用于 32 位代码的本机 dll)。在这种情况下,请使用下面介绍的解决方案。这也可能是没有充分理由的。在这种情况下,更改 xyz.dll.

的目标平台

程序 CSI.exe(C# 交互式编译器)的构建方式使其 运行 在 Windows 的 32 位版本上作为 32 位可执行文件 运行 在 Windows.

的 64 位版本中作为 64 位可执行文件

当 运行ning 作为 64 位可执行文件时,它将无法加载在 32 位进程中仅针对 运行 构建的程序集(例如您的 xyz.dll) .


为了改变这一点,您可以创建一个 CSI.exe 版本,即使在 Windows 的 64 位版本上,该版本 运行 也是 32 位进程。请按照以下步骤操作:

  1. 找到文件 csi.exe,在同一目录中复制一份并将副本重命名为 csi32.exe。您可以在类似于 "C:\Program Files (x86)\MSBuild.0\Bin\csi.exe"
  2. 的路径中找到 csi.exe
  3. 找到文件 CorFlags.exe。您可以在类似于 "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools\CorFlags.exe"
  4. 的路径中找到 CorFlags.exe
  5. 以管理员身份打开命令行 window。
  6. 使用两个可执行文件的正确路径执行以下命令

    C:\Path\To\CorFlags.exe /32bit+ /force C:\Path\To\csi32.exe

您现在可以使用 csi32.exe 代替 csi.exe 到 运行 需要 assemblies/dlls 的脚本,这些脚本仅在 32 位版本中可用。