我们的测试机器上有个很奇怪的bug。错误是:

系统。来自程序集“activeviewer(…)”的类型“DummyItem”中的方法“SetShort”没有实现。

我就是不明白为什么。SetShort在DummyItem类中,我甚至重新编译了一个版本,写入事件日志,只是为了确保它不是部署/版本控制问题。奇怪的是,调用代码甚至不调用SetShort方法。


当前回答

在最近的一次windows更新后,我收到了这个错误。我设置了一个从接口继承的服务类。该接口包含一个返回ValueTuple的签名,这是c#中的一个相当新的特性。

我所能猜到的是,windows更新安装了一个新的,但即使显式引用它,更新绑定重定向,等等…最终的结果只是将方法的签名更改为“标准”的东西。

其他回答

我在运行单元测试时也遇到了这个问题。应用程序运行良好,没有错误。 在我的案例中,问题的原因是我关闭了测试项目的构建。 重新启用我的测试项目的构建解决了这些问题。

除了提问者自己已经说过的答案之外,还有以下几点值得注意。发生这种情况的原因是,类可能拥有与接口方法具有相同签名的方法,而无需实现该方法。下面的代码说明:

public interface IFoo
{
    void DoFoo();
}

public class Foo : IFoo
{
    public void DoFoo() { Console.WriteLine("This is _not_ the interface method."); }
    void IFoo.DoFoo() { Console.WriteLine("This _is_ the interface method."); }
}

Foo foo = new Foo();
foo.DoFoo();               // This calls the non-interface method
IFoo foo2 = foo;
foo2.DoFoo();              // This calls the interface method

I got this error because I had a class in an assembly 'C' which was on version 4.5 of the framework, implementing an interface in assembly 'A' which was on version 4.5.1 of the framework and serving as the base class to assembly 'B' which was also on version 4.5.1 of the framework. The system threw the exception while trying to load assembly 'B'. Additionally, I had installed some nuget packages targeting .net 4.5.1 on all three assemblies. For some reason, even though the nuget references were not showing in assembly 'B', it was building successfully.

事实证明,真正的问题是程序集引用了包含接口的nuget包的不同版本,并且接口签名在不同版本之间发生了变化。

在我的例子中,我以前在repo之外的兄弟文件夹中引用了一个mylib项目——让我们称之为v1.0。

|-- myrepo
|    |-- consoleApp
|    |-- submodules
|         |-- mylib (submoduled v2.0)
|-- mylib (stale v1.0)

后来我做了正确的,并通过一个git子模块使用它-让我们称之为v2.0。 然而,一个项目consoleApp没有正确更新。它仍然在我的git项目之外引用旧的v1.0项目。

令人困惑的是,尽管*。csproj显然是错误的,并且指向v1.0, Visual Studio IDE将路径显示为v2.0项目! F12来检查接口和类也去了v2.0版本。

编译器放在bin文件夹中的程序集是v1.0版本,因此令人头疼。

IDE对我撒谎的事实让我很难意识到这个错误。

解决方案:从ConsoleApp中删除项目引用并读取它们。

一般提示:从头重新编译所有程序集(如果可能的话,当然不能用于nuget包),并检查bin\debug文件夹中的日期时间戳。任何过时的程序集都是你的问题。

这仅仅意味着实现项目在我的案例中已经过时了。重新构建了包含接口的DLL,但实现DLL陈旧。