我试图使用c# 4.0编译我的excel插件,并在Visual Studio中构建我的项目时开始遇到这个问题。重要的是要告诉你,我以前从来没有遇到过这个问题。什么会导致这种情况发生?


当前回答

如果您更改了程序集版本或复制了错误中声明的托管库的不同版本,则以前编译的文件也可能引用了错误的版本。一个“重建所有”(或删除你的“bin”和“obj”文件夹在前面的评论中提到)应该解决这个问题。

其他回答

您需要使用密钥对程序集进行签名。进入选项卡签名下的项目属性:

您的程序集是否正确签名?

要检查这一点,在项目上按Alt+Enter(或右键单击,然后Properties)。点击“签名”。验证复选框“签名程序集”已选中,强名称密钥文件已选中,且“仅延迟签名”未选中。

I recently hit this problem. In my case, I have NuGet packages on different assemblies. What I had was different versions of the same NuGet packages associated with my own assemblies. My solution was to use the NuGet package manager upon the Solution, as opposed to the individual projects. This enables a "consolidation" option, where you can upgrade your NuGet packages across as many projects as you want - so they all reference the same version of the assembly. When I did the consolidations, the build failure disappeared.

如果您更改了程序集版本或复制了错误中声明的托管库的不同版本,则以前编译的文件也可能引用了错误的版本。一个“重建所有”(或删除你的“bin”和“obj”文件夹在前面的评论中提到)应该解决这个问题。

就我而言,我将项目升级到。net 4.7.2,但仍然使用旧的visual studio版本(2015年)构建。 当我在VS 2019中构建项目时,构建失败消失了。