我得到一个:

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

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

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

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


当前回答

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应用程序,但这个问题可能发生在不同类型的项目中。

其他回答

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

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

我不知道为什么这样工作,但我删除了VS2015告诉我它找不到的项目引用,并再次添加它。解决了问题。我尝试过清理、构建和重新启动VS,但都无济于事。

您还可以尝试删除您认为有问题的代码,并查看是否在没有引用该代码的情况下进行编译。如果不是,请修复问题,直到它再次编译,然后将您怀疑的问题代码重新插入。有时,当编译器不喜欢其他东西时,我就会得到一些我知道是正确的类或方法的奇怪错误。一旦我修复了它真正挂起来的东西,这些“幻影”错误就消失了。

在我的案例中,问题是在将命名空间更改为与另一个项目中的名称完全相同之后(有意地),程序集的名称也被VS更改了,因此有两个具有相同名称的程序集,其中一个覆盖了另一个

重新安装nuget包对我来说很有用。在我将. net Framework版本更改为与所有项目同步后,一些nuget包(特别是Entity Framework)仍然为以前的版本安装。这个命令在包管理器控制台中为整个解决方案重新安装包:

Update-Package –reinstall