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

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

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


当前回答

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

其他回答

另一种情况是,有符号程序集的版本不正确。这不是这种疾病的正常症状,但这是我患病的情况

asp.net项目包含程序集A和程序集B, B是强命名的 程序集A使用Activator。CreateInstance加载程序集C(即没有对单独构建的C的引用) C是引用程序集B的较旧版本构建的

希望这能帮助到一些人,我花了很长时间才弄明白。

我在重命名一个由ASP依赖的项目(和程序集名称)时遇到了这个问题。网络项目。类型在依赖程序集中实现了接口。尽管从“生成”菜单执行“清洁解决方案”,但具有先前名称的程序集仍保留在bin文件夹中,并且当我的web项目执行时

var types = AppDomain.CurrentDomain.
   GetAssemblies().
   ToList().
   SelectMany( s => s.GetTypes() /* exception thrown in this call */ )
;

上面的异常被抛出,抱怨实现web类型中的接口方法实际上没有实现。手动删除web项目的bin文件夹中的程序集解决了这个问题。

在我的例子中,它帮助重置WinForms工具箱。

当在设计器中打开窗体时,我得到了异常;然而,编译和运行代码是可能的,并且代码的行为符合预期。异常发生在实现我引用的库之一的接口的本地UserControl中。更新此库后出现错误。

这个UserControl列在WinForms工具箱中。可能Visual Studio保留了库的一个过时版本的引用,或者在某个地方缓存了一个过时的版本。

下面是我如何从这种情况中恢复过来的:

右键单击WinForms工具箱,然后单击上下文菜单中的重置工具箱。(这将从工具箱中删除自定义项)。 在我的例子中,“工具箱”项被恢复到默认状态;但是,“工具箱”中缺少指针箭头。 关闭Visual Studio。 在我的案例中,Visual Studio以一个违反异常终止并中止。 重新启动Visual Studio。 现在一切都很顺利。

注:如果这个答案对你没有帮助,请花点时间向下滚动人们添加的其他答案。

简短的回答

如果将方法添加到一个程序集中的接口,然后再添加到另一个程序集中的实现类,但在没有引用接口程序集中的新版本的情况下重新构建实现程序集中,就会发生这种情况。

在本例中,DummyItem实现了来自另一个程序集的接口。SetShort方法最近被添加到接口和DummyItem中—但是包含DummyItem的程序集是引用先前版本的接口程序集重新构建的。因此SetShort方法有效地存在,但没有将其链接到接口中的等效方法。

长回答

如果你想尝试复制这个,试试下面的方法:

Create a class library project: InterfaceDef, add just one class, and build: public interface IInterface { string GetString(string key); //short GetShort(string key); } Create a second class library project: Implementation (with separate solution), copy InterfaceDef.dll into project directory and add as file reference, add just one class, and build: public class ImplementingClass : IInterface { #region IInterface Members public string GetString(string key) { return "hello world"; } //public short GetShort(string key) //{ // return 1; //} #endregion } Create a third, console project: ClientCode, copy the two dlls into the project directory, add file references, and add the following code into the Main method: IInterface test = new ImplementingClass(); string s = test.GetString("dummykey"); Console.WriteLine(s); Console.ReadKey(); Run the code once, the console says "hello world" Uncomment the code in the two dll projects and rebuild - copy the two dlls back into the ClientCode project, rebuild and try running again. TypeLoadException occurs when trying to instantiate the ImplementingClass.

另一种可能是在依赖关系中混合发布和调试构建。例如,程序集A依赖于程序集B, A是在调试模式下构建的,而GAC中的B副本是在发布模式下构建的,反之亦然。