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

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

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


当前回答

我在Visual Studio Pro 2008中看到了这一点,当时两个项目构建了具有相同名称的程序集,一个是类库SDF.dll,另一个是使用程序集名称sdf.exe引用程序库。 当我更改引用程序集的名称时,异常消失了

其他回答

这类问题的另一种解释涉及托管c++。

如果您试图存根使用托管c++创建的具有特殊签名的程序集中定义的接口,您将在创建存根时得到异常。

对于Rhino mock和任何使用System.Reflection.Emit的mock框架都是如此。

public interface class IFoo {
  void F(long bar);
};

public ref class Foo : public IFoo {
public:
  virtual void F(long bar) { ... }
};

接口定义获得以下签名:

void F(System.Int32 modopt(IsLong) bar)

注意,c++类型long映射到System。Int32(或者在c#中简称为int)。正如Ayende Rahien在Rhino Mocks邮件列表中所述,这是一个有点晦涩的modopt导致的问题。

Our problem solved with updating windows! Our web application is on .Net 4.7.1 and c# 7.0. As we tested in different windowses, we understood that the problem will be solved by updating windows. Indeed, the problem was seen in windows 10 (version 1703) and also in a windows server 2012(not updated in last year). After updating both of them, the problem was solved. In fact, the asp.net minor version(the third part of the clr.dll version in C:\Windows\Microsoft.NET\Framework64\v4.0.30319 ) was changed a bit after the update.

对我来说,解决方案与实现接口的项目设置了“Register for COM Interop”属性有关。取消勾选这个选项为我解决了这个问题。

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

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

我在WCF服务中得到了这个,因为选择了x86构建类型,导致箱子在bin\x86下而不是bin下。选择Any CPU会导致重新编译的dll到达正确的位置(我不会详细说明这是如何发生的)。