我得到一个:

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

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

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

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


当前回答

把我的解决方案加入到混合中,因为它有点不同,我花了一段时间来弄清楚。

In my case I added a new class to one project but because my version control bindings weren't set I needed to make the file writable outside of Visual Studio (via the VC). I had cancelled out of the save in Visual Studio but after I made the file writable outside VS I then hit Save All again in VS. This inadvertently caused the new class file to not be saved in the project..however..Intellisense still showed it up as blue and valid in the referencing projects even though when I'd try to recompile the file wasn't found and got the type not found error. Closing and opening Visual Studio still showed the issue (but if I had taken note the class file was missing upon reopening).

当我意识到这一点时,解决方法很简单:将项目文件设置为可写,将丢失的文件读入项目。现在都建好了。

其他回答

对于那些在试图将网站发布到Azure时遇到这种错误的人来说,上面这些看起来很有希望的解决方案都没有帮助到我。我也在同一条船上——我的解决方案本身构建得很好。最后我不得不这么做

把我溶液里的鸡块都去掉。 关闭并重新打开我的溶液。 重新添加所有的nuget包。

有点痛苦,但这是我能让我的网站发布到Azure的唯一方法。

从GAC中删除程序集(C:\WINDOWS\assembly文件夹-选择您的程序集,右键单击并卸载)。因为解决方案使用guid保持引用,如果guid在GAC中,它将继续接受GAC版本进行编译。

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

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

为此挣扎了一段时间,而且不是第一次了。在过去,这通常是配置不匹配造成的,但这次不是。

这一次,在我的应用程序中,自动生成绑定重定向被设置为true。

实际上,如果我在我的库中将它设置为true,那么我的库就会在Microsoft中的类型中得到这个错误。报告名称空间,如果在我的应用程序中将它设置为true,那么我的应用程序将从我的库中获取类型的此错误。

此外,对于我的库,该值似乎默认为false,但对于我的应用程序,该值为true。因此,如果我不指定任何一个,我的库构建正常,但我的应用程序得到了我的库的错误。所以,我必须在我的应用程序中明确地将其设置为false,否则我会得到这个错误,没有说明原因。

我还发现,如果项目没有使用sdk csproj,无论设置如何,我都会收到这条消息。一旦我转换到sdk csproj,设置就会有所不同。

同样,在我的例子中,这一切似乎都与Microsoft.ReportingServices.ReportViewerControl.Winforms nuget包有关,它在我的根库中。