我得到一个:

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

错误的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).

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

其他回答

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

在将一些新代码合并到vs2019项目后遇到了同样的问题。 重新启动VS,卸载和重新加载项目,确保解决方案中所有项目ToolsVersion=和TargetFrameworkVersion相同。这些都无济于事。

我有一个项目基板的情况,未能找到名称空间皮肤(在项目皮肤)。

最后我打开了基板。并检查了所有的ProjectReference Include条目。其他的都在那里,但没有对Skin的引用,即使Skin确实显示在小项目依赖对话框的复选框中。因此,项目依赖关系对话框接受了皮肤的复选框(并将其保存在某个地方),但没有改变basis .csproj。然后我手动添加ProjectReference Include,确保我拥有皮肤项目的正确路径和GUID。

< ProjectReference Include = " \ Skin皮肤csproj”>。 < > {1ad4b5d7-5014-4f5f-983e-2c59ac0e0028} < - >项目项目 < Name > Skin - < Name > < / ProjectReference >

然后我保存了基板。Csproj,问题就解决了。正如其他人所说,这是VS工具的一个问题

检查你的文件扩展名:

工作在vscode和一直得到相同的错误,但一切工作!彩色编码,智能感知,快速操作,文件夹图标....但出于某种原因,它就是建不起来。

最后浏览本地文件系统,注意到类文件上的文件扩展名丢失了,构建一直抱怨这个问题。

我们有一个奇怪的例子,我刚刚在溶液中修正了它。在主项目中的“using”语句前有一个隐藏/空白字符。这个项目可以很好地构建,网站也可以很好地运行,但是引用它的单元测试项目不能被构建。

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

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