如何修复编译器错误 CS0009 - 尝试加载格式不正确的程序?

How to fix the compiler error CS0009 - An attempt was made to load a program with an incorrect format?

我正在尝试 reference the Microsoft ActiveX Data Objects 6.1 Library (ADODB) using the csc.exe C# 编译器,但出现错误:

fatal error CS0009: Metadata file 'c:\Program Files\Common Files\system\ado\msado15.dll' could not be opened -- 'An attempt was made to load a program with an incorrect format. '

我已简化文件以尝试跟踪问题。

批处理文件Compile.bat:

@ECHO OFF
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe -nologo -r:"C:\Program Files\Common Files\system\ado\msado15.dll" Code.cs
PAUSE >NUL

C# 文件Code.cs:

class Program {
  static void Main() {
    ADODB.Connection ADODBConnection = new ADODB.Connection();
    System.Console.WriteLine((ADODBConnection == null).ToString());
  }
}

我已经尝试 copying the dll to the same folder 并引用它但得到了同样的错误。

我还有编译器版本 ...\v2.0.50727\csc.exe...\v3.5\csc.exe 以及库版本 msado20.tlbmsado21.tlbmsado25.tlbmsado26.tlbmsado27.tlbmsado28.tlbmsado60.tlb

当我在 Visual Studio 2017(.NET Framework 4 控制台应用程序)上尝试相同时,它起作用了。它创建 ConsoleApp1\ConsoleApp1\obj\Debug\Interop.ADODB.dll 并引用它,但我不知道它是从哪里得到的。我试过在不同的文件夹中搜索它。

我从 this thread (by searching online for Interop.ADODB.dll file location) and later this link:

找到 Visual Studio 使用的文件

C:\Program Files\Microsoft.NET\Primary Interop Assemblies\adodb.dll

工作Compile.bat

@ECHO OFF
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe -nologo -r:"%ProgramFiles%\Microsoft.NET\Primary Interop Assemblies\adodb.dll" Code.cs
PAUSE >NUL