我得到一个:

找不到类型或名称空间名称

错误的c# WPF应用程序在VS2010。这部分代码编译得很好,但突然就出现了这个错误。我已经尝试删除项目引用和using语句,关闭VS2010并重新启动,但我仍然有这个问题。

有什么想法,为什么这可能会发生,似乎我在做正确的事情re Reference & using语句?

我还注意到在VS2010,智能感知的名称空间是工作的好,所以它似乎VS2010有项目引用,是看到名称空间在一方面,但在编译期间没有看到它?


当前回答

在构建解决方案时,我得到了相同的错误(类型或名称空间' '找不到)。在它下面,我看到一个警告,声明“引用无法解析”,并确保“程序集存在于磁盘上”。

我非常困惑,因为我的DLL非常清楚地位于引用所指向的位置。VS似乎没有突出显示任何错误,直到我尝试构建解决方案。

我终于意识到问题所在(或者至少我怀疑是问题所在)。我在同一个解决方案中构建库文件。因此,即使它存在于磁盘上,它也在那个位置被重新构建(在库重新构建的过程中,我的另一个项目-在同一解决方案中-引用了库,必须确定库不存在)

当我右键单击项目并只构建该项目,而不是整个解决方案时,我没有得到错误。

为了解决这个问题,我将库作为依赖项添加到正在使用它的项目中。

这样做:

我在解决方案资源管理器中右键单击我的解决方案并选择 “属性” 然后在“Common Properties”中选择“Project Dependencies”。 然后在项目下拉菜单中,我选择了项目 依靠图书馆,而且 选中“依赖”下的库旁边的复选框

这确保首先构建库项目。

其他回答

I had a similar issue: The compiler was unable to detect a folder inside the same project, so a using directive linking to that folder generated an error. In my case, the problem originated from renaming the folder. Even though I updated the namespace of all the classes inside that folder, the project info somehow failed to update. I tried everything: deleting the .suo file and the bin and obj folders, cleaning the solution, reloading the project - nothing helped. I resolved the problem by deleting the folder and the classes inside, creating a new folder and creating new classes in that new folder (simply moving the classes inside the new folder didn't help).

PS:以我为例,我正在开发一个web应用程序,但这个问题可能发生在不同类型的项目中。

我遇到了同样的问题:VS 2017强调引用项目中的一个类为错误,但解决方案构建正常,甚至智能感知工作。

下面是我解决这个问题的方法:

卸载引用的项目 在VS中打开。proj文件(我正在寻找重复的人建议在这里) 重新加载项目(我没有改变甚至保存proj文件,因为我没有任何副本)

在我的情况下,我卸载项目,然后:

Opened myProject.csproj and update the ToolsVersion="4.0" to ToolsVersion="12.0"(I'm using vs 2017)(using Paulus's answer https://stackoverflow.com/a/64552201/1594487). Deleted following lines from the myProject.csproj: <Import Project="..\packages\EntityFramework.6.4.0\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

问题解决了。

在我的案例中,我有一个由外部依赖(xsd2code)构建的文件,不知为何它的designer.cs文件没有被VS正确处理。在Visual Studio中创建一个新文件并将代码粘贴进去,这对我来说很管用。

首先,我将验证您的项目生成的信息是否损坏。在解决方案上进行清理和重新构建。

如果这还不起作用,我在过去看到的一种解决设计器问题的方法是打开一个windows窗体项目,然后再次关闭它。这有点像鸡内脏,所以别抱太大希望。