System.IO.FileNotFoundException: 'Could not load file or assembly' 在本地机器上

System.IO.FileNotFoundException: 'Could not load file or assembly' on a local machine

我正在对我最近加入的一个项目实施单元测试,遇到了一个令人费解的错误。我有这个 class LoggerSettingsManager 具有我正在测试的各种功能。调用某些函数时一切正常。但是当调用相同的 class 但不同的函数时,我收到以下错误(该错误有一个内部代码 _COMPlusExceptionCode -532462766):

Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 
The system cannot find the file specified.

这些是 LoggerSettingsManager 需要的库:

很多功能似乎都很好。它似乎只有在函数引用 MessageBoxSystem.Windows.Forms 的一部分)时才会发生,即使这行代码从未真正执行过。

当我调用例如以下函数时出现问题。然而,让我有点困惑的是,在导致错误之前我什至无法进入函数,就好像函数的内容不是问题而是与 class.

相关联的一些库
public void UpdateLoggerSettings(string measurementType)
{
    ...

    try
    {
        _deviceSettings.GetLoggerSettings(LoggerSettings);

        if (LoggerSettings.DataIsValid)
            _deviceSettingsWin.UpdateWindowParameter();
    }
    catch (ArgumentException ex)
    {
        MessageBox.Show("Requesting Data Logger Settings: Packet contained invalid time stamps\n\n" +
            ex.Message + "\n\n" + ex.StackTrace);
    }
}

这些是LoggerSettingsManager中的usings

using ExpoMRF4Utility.ExpomRF4; // internal company library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

我是否需要“全新安装”System.Windows.Forms?我看过有关此错误的其他文章,但似乎很多问题都与服务器-客户端相关(我的问题绝对 不是 )或以其他方式特定。帮助表示赞赏。

编辑:删除和添加引用未解决问题..

编辑 2:在 pastebin 上添加最少的可重现文件:LoggerSettingsManager, LoggerSettingsManager_Tests

我似乎已经能够通过创建不同的测试项目来解决问题。在下图中,有 Visual Studio 提供的用于创建测试项目的选项。之前我选择了一个 xUnit Test Project 结果只有 运行 .NET Core 并且不可能引用与被测项目相同版本的 .NET,或者引用“缺失的”库 System.Windows.Forms.

现在我创建了一个 Unit Test Project (.NET Framework),同时添加了 xUnit 并删除了 MSTest。这似乎已经成功了。我希望这对遇到类似问题的人有所帮助。