我们的测试机器上有个很奇怪的bug。错误是:
系统。来自程序集“activeviewer(…)”的类型“DummyItem”中的方法“SetShort”没有实现。
我就是不明白为什么。SetShort在DummyItem类中,我甚至重新编译了一个版本,写入事件日志,只是为了确保它不是部署/版本控制问题。奇怪的是,调用代码甚至不调用SetShort方法。
我们的测试机器上有个很奇怪的bug。错误是:
系统。来自程序集“activeviewer(…)”的类型“DummyItem”中的方法“SetShort”没有实现。
我就是不明白为什么。SetShort在DummyItem类中,我甚至重新编译了一个版本,写入事件日志,只是为了确保它不是部署/版本控制问题。奇怪的是,调用代码甚至不调用SetShort方法。
当前回答
我在重命名一个由ASP依赖的项目(和程序集名称)时遇到了这个问题。网络项目。类型在依赖程序集中实现了接口。尽管从“生成”菜单执行“清洁解决方案”,但具有先前名称的程序集仍保留在bin文件夹中,并且当我的web项目执行时
var types = AppDomain.CurrentDomain.
GetAssemblies().
ToList().
SelectMany( s => s.GetTypes() /* exception thrown in this call */ )
;
上面的异常被抛出,抱怨实现web类型中的接口方法实际上没有实现。手动删除web项目的bin文件夹中的程序集解决了这个问题。
其他回答
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包的不同版本,并且接口签名在不同版本之间发生了变化。
我也有这个错误,这是由任何CPU exe引用的任何CPU程序集,反过来引用x86程序集引起的。
异常抱怨MyApp中类的一个方法。实现(任何CPU),它派生了MyApp。接口(任何CPU),但在fuslogvw.exe中,我发现了一个隐藏的“试图从MyApp加载格式不正确的程序”异常。CommonTypes (x86),两者都使用它。
当我的集成测试项目试图加载一个不包含接口依赖项解析的DLL时,我遇到了这个错误:
集成测试项目(参考主项目,但不是 StructureMap) 主要项目(引用StructureMap项目-使用 类构造函数中的接口) StructureMap项目(IoC - For().Use();)
这将导致抛出错误,因为它无法找到具体的实现。我在测试配置中排除了DLL,错误消失了
注:如果这个答案对你没有帮助,请花点时间向下滚动人们添加的其他答案。
简短的回答
如果将方法添加到一个程序集中的接口,然后再添加到另一个程序集中的实现类,但在没有引用接口程序集中的新版本的情况下重新构建实现程序集中,就会发生这种情况。
在本例中,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.
在我的例子中,我试图使用TypeBuilder创建一个类型。TypeBuilder。CreateType抛出此异常。我最终意识到我需要添加MethodAttributes。在调用TypeBuilder时,将属性虚拟化。帮助实现接口的方法的DefineMethod。这是因为如果没有这个标志,该方法不会实现接口,而是实现一个具有相同签名的新方法(甚至没有指定MethodAttributes.NewSlot)。