我得到一个:

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

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

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

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


当前回答

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

其他回答

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

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).

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

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

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 . net Core 2.2 Razor Pages,我觉得这个答案可能会帮助到一些人。 如果是蛇,它会咬我的。 我到处乱扔东西,更改名称,重命名模型,突然我得到了这个错误:

错误CS0246类型或命名空间名称不能为“UploadFileModel” 找到(是否缺少using指令或程序集引用?)

这在我的。chstml Razor Page中用红色划线。(修正后未加下划线):

@page
@model UploadFileModel

所以,最后,幸运的是,我从其他人那里找到了我最初使用的代码,注意,名称空间不包括.cshtml文件名!!

这是我的坏假错误在命名空间的页面名称打我自己:

namespace OESAC.Pages.UploadFile
{
    public class UploadFileModel : PageModel
    {

我的原始代码所拥有的,我所要做的就是从命名空间UploadFile中删除页面名称:

namespace OESAC.Pages
{
    public class UploadFileModel : PageModel
    {

瞧,所有的错误都消失了!! 愚蠢的我。但是你知道,微软让。net c# MVC的东西让我们这些非计算机科学家非常困惑。我经常被鞋带绊倒,试图找出模型名称、页面名称和使用它们的语法。不应该这么难。哦。我希望错误和解决方案能帮助别人。错误是正确的,没有命名空间名为“UploadFileModel”哈哈。